安装nginx
brew install nginx
brew是mac下应用管理工具,如果没安装过可通过终端运行
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
进行安装。
nginx.conf文件介绍
配置文件位置一般位于/usr/local/etc/nginx/
(可使用nginx -t
来查看配置文件位置)
对nginx的应用都基于
nginx.conf
,那么我们是不是有必要来了解一下它的用途?当然。
#nginx运行的用户和用户组
#user xxx xxx
#nginx进程数
worker_processes 1;
#错误日志保存路径
error_log /Users/wangzhidong/logs/nginx/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#进程文件
#pid logs/nginx.pid;
#工作模式与连接数上限
events {
#单个进程最大连接数(最大连接数=连接数*进程数)
worker_connections 1024;
}
#设定http服务器
http {
#文件扩展名与文件类型映射表
include mime.types;
#默认文件类型
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /Users/wangzhidong/logs/nginx/access.log main;
#开启高效文件传输模式,sendfile指令指定nginx是否调用sendfile函数来输出文件,对于普通应用设为on,如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络I/O处理速度,降低系统负载。注意:如果图片显示不正常把这个改成off。
sendfile on;
#防止网络阻塞
#tcp_nopush on;
#长连接超时时间,单位是秒
#keepalive_timeout 0;
keepalive_timeout 65;
#开启gzip压缩输出
#gzip on;
#虚拟主机的配置
server {
#监听端口
listen 8080;
#域名可以有多个,用空格隔开
server_name localhost;
#默认编码
#charset koi8-r;
#定义本虚拟主机的访问日志
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
#引入servers下的配置文件
include servers/*;
}
查看群组注:
nginx.conf
文件默认没有设置用户和用户组,使用前需自行添加,一般会在nginx.conf
文件首行添加user xxx(用户名) xxx(用户组);
(例:user wangzhidong staff;
),否则会因没有权限被拒绝访问。
用户组可在nginx
目录下执行groups
命令查看,取第一个即可。
错误排查机制
如果真的出错了我们怎么知道?这里来引申说一下nginx错误排查机制,我们以上述没有用户名的情况来举个栗子:
首先找到nginx的error.log错误日志路径(
nginx.conf
文件的error_log
对应路径); 然后终端执行tail -f /error.log文件路径
(tail -f实时查看日志文件);最后修改错误,在nginx.conf
首行添加用户及用户组。终端输出如下:
实时查看日志
我们可以看到日志中显示
Permission denied
无权限访问被拒绝。
添加测试站点配置文件
根据nginx.conf
尾行的include servers/*;
我们得知默认servers
文件夹下的文件会被引入,所以我们可以在其中添加我们所需的测试站点的配置文件,举例操作如下:
进入servers
文件夹下新建本地测试文件,(例:local.test.com.conf)
文件内容如下:
server
{
listen 80;
#要访问的域名
server_name local.test.com;
location / {
add_header Cache-Control no-cache;
index index.html index.htm index.php;
#测试项目所在路径
root /Users/wangzhidong/Documents/testDocuments/src;
}
access_log /Users/wangzhidong/logs/nginx/access.log;
}
这里我们只要修改server_name和root即可,这样nginx相关的配置就完成了,接下来修改hosts文件。
配置本地hosts文件
正常我们用域名访问网页时通常会先进行dns解析,将域名解析成ip,然后和ip所在的机器建立网络连接(详见从输入URL到页面加载发生了什么),那么我们是否可以不进行dns解析直接获取ip?将域名和本地ip绑定可实现这一步骤。计算机网络中,本地回环地址指的是以127开头的地址(127.0.0.1 - 127.255.255.254),通常用127.0.0.1来表示,所以在hosts
文件里添加映射关系将server_name
对应的域名和本地ip127.0.0.1
绑定即可。操作如下:
mac的hosts文件位于 /private/etc 目录下, hosts 文件中可以手动添加域名和 IP 的映射关系。
##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting. Do not change this entry.
##
127.0.0.1 localhost
255.255.255.255 broadcasthost
::1 localhost
127.0.0.1 local.test.com
最后重启nginxsudo nginx -s reload
即可。
网友评论