基本配置
配置文件基本结构
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
|
...
events { ... }
http { ... server { ... location [PATTERN] { ... } location [PATTERN] { ... } } server { ... } ... }
|
全局块
配置影响nginx全局的指令。一般有运行nginx服务器的用户组,nginx进程pid存放路径,日志存放路径,配置文件引入,允许生成worker process数等。
1 2 3 4 5 6 7 8 9
| worker_processes 1;
|
events块
配置影响nginx服务器或与用户的网络连接。有每个进程的最大连接数,选取哪种事件驱动模型处理连接请求,是否允许同时接受多个网路连接,开启多个网络连接序列化等。
1 2 3 4 5
| events { keepalive_timeout 65; worker_connections 1024; }
|
http块
可以嵌套多个server,配置代理,缓存,日志定义等绝大多数功能和第三方模块的配置。如文件引入,mime-type定义,日志自定义,是否使用sendfile传输文件,连接超时时间,单连接请求数等。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| http {
default_type text/plain; keepalive_timeout 65; server_names_hash_bucket_size 128; client_header_buffer_size 32k; large_client_header_buffers 4 64k; client_max_body_size 8m; gzip on; gzip_min_length 1k; gzip_buffers 4 16k; gzip_http_version 1.1; gzip_comp_level 2; gzip_types text/plain application/x-javascript text/css application/xml; gzip_vary on; }
|
server块
配置虚拟主机的相关参数,一个http中可以有多个server。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| http { ... server { keepalive_requests 120; listen 4545; server_name 127.0.0.1; location ~*^.+$ { deny 127.0.0.1; allow 192.168.0.0; } } server { ... }
}
|
location块
配置请求的路由,以及各种页面的处理情况。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| http { ...
server { ... location ~*^.+$ { alias path; proxy_pass http://mysvr; deny 127.0.0.1; allow 172.18.5.54; try_files $uri $uri/ /index.html; } location /oauth/{ proxy_pass https://192.168.0.1:8001/oauth/; proxy_set_header HOST $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ { expires 10d; } location ~ .*\.(js|css)?$ { expires 1h; } } }
|
举个例子(前端本地访问静态文件-用于解决跨域问题)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
|
worker_processes 2;
events { worker_connections 1024; }
http { gzip on; gzip_comp_level 4; gzip_vary on; gzip_types image/png text/plain application/javascript text/css application/xml font/woff text/javascript; server { listen 8001; server_name localhost; location / { root D:/wn244/test; index index.html index.htm; try_files $uri $uri/ /index.html; } location /static { alias D:/wn244/test/static; } location /api { proxy_pass http://ip:port; } }
}
|
nginx常用命令
1 2 3 4 5 6 7
| nginx -c /usr/local/nginx/conf/nginx.conf nginx -s quit nginx -s reload nginx -s reopen nginx -v nginx -t nginx -h
|
windows杀死所有nginx进程
1
| taskkill /f /t /im nginx.exe
|
具体功能介绍
rewrite
rewrite 对用户的 URI 用正则表达式的方式进行重写,并跳转到新的 URI。语法:rewrite regex replacement [flag];
重点:replacement 是重写 URI 的改写规则。当改写规则以”http://“”https://“或”$scheme”开头时,Nginx 重写该语句后将停止执行后续任务,并将改写后的 URI 跳转返回客户端。
这句话的意思是:当replacement 是以”http://“”https://“或”$scheme”开头时,在浏览器端访问的网址会直接变化,变成replacement所表示的地址。而当replacement 不已以上规则开头时,访问的页面文件会根据规则发送变化,但是在浏览器端显示网址是不会发送变化的,以下是具体情形:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| http { ...
server { ... loaction / { root D:/wn244/test; index index.html index.htm; } location =/login { rewrite /login /; } location =/home { rewrite /home https://www.baidu.com; } } }
|