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; } }
