nginx问题相关记录

nginx目前主要用来做反向代理和负载均衡,其实它也可以是一个web服务器;

1、反向代理:

location /api/ {
proxy_next_upstream error timeout http_503 http_502 http_504;
proxy_pass http://myweb1-server/api/;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;

}

2、七层的负载均衡:

upstream web1 {
server 127.0.0.1:111 weight=1;
server 127.0.0.1:222 weight=1;
}
upstream web2 {
server 127.0.0.2:111 weight=1;
server 127.0.0.2:222 weight=6;
server 127.0.0.2:333 weight=7;
}


3、nginx中root与alias的区别:

1、root的处理结果是:root路径+location路径
alias的处理结果是:使用alias路径替换location路径

例如:location /a/{

                          root /data/www;

                         }

访问http://dlgde.cn/a/page.html,root会对应到资源/data/www/a/page.html,也就是root路径+location路径;

而location /a/{

                          alias /data/www;

                         }

同样请求http://dlgde.cn/a/page.html,请求资源查找路径是 /data/www/page.html,不管location怎默写,都是去alias指定的路径。

2其他区别:

  1. alias 只能作用在location中,而root可以存在server、http和location中。
  2. alias 后面必须要用 “/” 结束,否则会找不到文件,而 root 则对 ”/” 可有可无 

详情参考;

4、获取客户端的真实ip

跟proxy_set_header配置有关,具体参考

5、nginx手机跳转设置,

加个条件判断,是手机请求,就rewrite到手机相应的网址。例如:

location / {
if ($http_user_agent ~* "(Android|iPhone|iPod|Symbian|BlackBerry|Windows Phone|Mobile|J2ME)") {
rewrite ^ http:stage01.dlgde.cn permanent;
}
root /data/www/stage01/dlgde/;
index index.html index.htm;
try_files $uri $uri/ /index.html;
expires 5;
}

6、nginx中的try_files 使用:

如5中提到的,这个指令,其作用是按顺序检查文件是否存在,返回第一个找到的文件或文件夹(结尾加斜线表示为文件夹),如果所有的文件或文件夹都找不到,会进行一个内部重定向到最后一个参数。但最后一个一定不能为空,不然会进入死循环而报500的错误,参考

其他待记录。。。

猜你喜欢

转载自www.cnblogs.com/qstudy/p/9926572.html