1. 常用命令
验证配置是否正确: nginx -t
查看Nginx的版本号:nginx -v
启动Nginx:start nginx
快速停止或关闭Nginx:nginx -s stop
正常停止或关闭Nginx:nginx -s quit
配置文件修改重装载命令:nginx -s reload
2. server {...}配置
server {
# 监听端口
listen 80;
# 监听地址,一般为域名。e.g. www.baidu.com
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
# 代理地址, 指向服务器ip地址
proxy_pass http://127.0.0.1:8080;
# 设置默认页
index index.html index.htm;
}
# 错误页
error_page 404 www.test.com;
}
3. location {...}配置
-
=:精确匹配
location = / {...},e.g.test.com/✔test.comx -
^~:uri以某个常规字符串开头
location ^~ /static/ {...},e.g.test.com/static/a.jpg✔test.com/static/b.jpg✔ -
~:区分大小写的正则匹配,!~: 表否定, 不匹配
location ~ /get/ {...},e.g.test.com/get/✔test.com/Get/x -
~*:不区分大小写的正则匹配,!~*: 表否定, 不匹配
location ~* /get/ {...},e.g.test.com/get/✔test.com/Get/✔ -
/:通用匹配,如果没有其它匹配,任何请求都会匹配到
location / {...}
优先级顺序:
=>完整路径>^~>~,~* 正则>部分起始路径>/
4. rewrite语法
rewrite <regex> <replacement> [flag];
关键字 正则 替代内容 flag标记
flag标记说明:
last 本条规则匹配完成后,继续向下匹配新的location URI规则
break 本条规则匹配完成即终止,不再匹配后面的任何规则
redirect 返回302临时重定向,浏览器地址会显示跳转后的URL地址
permanent 返回301永久重定向,浏览器地址栏会显示跳转后的URL地址
e.g.
rewrite ^/(.*) http://www.baidu.com/$1 permanent;
^/(.*) ,匹配完整的域名和后面的路径地址
$1,是取自正则()里的内容
5. 全局变量
e.g. http://localhost:88/test1/test2/test.php
$host:localhost
$server_port:88
$request_uri:http://localhost:88/test1/test2/test.php
$document_uri:/test1/test2/test.php
$document_root:D:\nginx/html
$request_filename:D:\nginx/html/test1/test2/test.php











网友评论