nginx常用命令
sudo nginx // 开启nginx服务器 sudo nginx -s reload // 重启nginx服务器 sudo nginx -s stop // 关闭nginx nginx -t // 检查nginx配置,如果出现以下提示表示配置成功 nginx: the configuration file /usr/local/etc/nginx/nginx.conf syntax is ok nginx: configuration file /usr/local/etc/nginx/nginx.conf test is successful // 命令行参数(参考资料:http://nginx.org/en/docs/switches.html) -? | -h — print help for command-line parameters. -c file — use an alternative configuration file instead of a default file. -g directives — set global configuration directives, for example, nginx -g "pid /var/run/nginx.pid; worker_processes `sysctl -n hw.ncpu`;" -p prefix — set nginx path prefix, i.e. a directory that will keep server files (default value is /usr/local/nginx). -q — suppress non-error messages during configuration testing. -s signal — send a signal to the master process. The argument signal can be one of: stop — shut down quickly quit — shut down gracefully reload — reload configuration, start the new worker process with a new configuration, gracefully shut down old worker processes. reopen — reopen log files -t — test the configuration file: nginx checks the configuration for correct syntax, and then tries to open files referred in the configuration. -T — same as -t, but additionally dump configuration files to standard output (1.9.2). -v — print nginx version. -V — print nginx version, compiler version, and configure parameters.
nginx代理配置
proxy_pass
在nginx中配置proxy_pass时:
如果是按照^~匹配路径时,要注意proxy_pass后的url最后的/,当加上了/,相当于是绝对根路径,则nginx不会把location中匹配的路径部分代理走;
如果没有/,则会把匹配的路径部分也给代理走。
location ^~ /support/ { proxy_cache js_cache; proxy_set_header Host www.xudengwei.com; proxy_pass http://www.xudengwei.com/; }
如上面的配置,如果请求的url是http://servername/support/test.html
会被代理成http://www.xudengwei.com/test.html
而如果这么配置
location ^~ /support/ { proxy_cache js_cache; proxy_set_header Host www.xudengwei.com; proxy_pass http://www.xudengwei.com; }
则会被代理到http://www.xudengwei.com/support/test.htm
rewrite
2.1 我们可以用如下的rewrite来实现上述/的功能
// 匹配任何以/support/开头的请求
location ^~ /support/ { proxy_cache js_cache; proxy_set_header Host www.xudengwei.com; rewrite /support/(.+)//1 break; proxy_pass http://www.xudengwei.com; }
2.2 rewrite中的$1/$2/$3...
server { listen 80; server_name dev.xudengwei.com; location / {: // 输入dev.xudengwei.com/test1/baidu 会重定向到 www.baidu.com,这里的$1就是上一个正则匹配的结果值 rewrite /test1/(.*) www.$1.com break; } }
last和break、permanent
last:last 和 break一样 它们都会终止此 location 中其他它rewrite模块指令的执行,但是 last 立即发起新一轮的 location 匹配 而 break 则不会
permanent: 永久性重定向。请求日志中的状态码为301
location
server { server_name website.com; location = /abcd { ...... } } 测试: http://website.com/abcd # 正好完全匹配 http://website.com/ABCD # 如果运行 Nginx server 的系统本身对大小写不敏感,比如 Windows ,那么也匹配 http://website.com/abcd?param1m2 # 忽略查询串参数,这里就是 /abcd 后面的 ?param1m2 http://website.com/abcd/ # 不匹配,因为末尾存在反斜杠 http://website.com/abcde # 不匹配,因为不是完全匹配
项目cases
// 以下的nginx方向代理配置用于将浏览器请求代理到本地服务器
server { listen 80; server_name dev.xudengwei.com; location / {
# 相对路径是dll,assets开头的,都会被代理到proxy_pass定义的host,如果是全路径都走local,rewrite就不用写了,直接proxy_pass rewrite '^(/(dll/|assets/).*)$' $1 break; proxy_pass http://127.0.0.1:3000; } location ^~ /leo/ { # dev proxy_set_header Host dev.xudengwei.com; proxy_pass http://39.106.39.185; } }