Nginx faq整理

Nginx如何替换错误

来源:http://stackoverflow.com/questions/5950996/how-to-replace-nginx-errors

Q:能否用503错误(负载过高或临时不可用)来代替502错误作为应答返回?
A:配置fastcgi_intercept_errors指令并将其设置为on,然后使用error_page指令:

location / {
    fastcgi_pass 127.0.0.1:9001;
    fastcgi_intercept_errors on;
    error_page 502 =503 /error_page.html;
    # ...
}

Nginx如何重写404到其他页面

来源:http://stackoverflow.com/questions/5920081/how-to-rewrite-if-file-not-found-using-nginx

Q:能否将请求的不存在文件重写到index.php?
A:使用try_files指令:

try_files $uri $uri/ /index.php

$uri与$uri/将判断请求的uri是否为一个存在的文件或目录,如果不是,将被重写到最后一个参数,即index.php

Nginx如何为代理的请求保存请求的Host头

来源:http://stackoverflow.com/questions/5834025/how-to-preserve-request-url-with-nginx-proxy-pass

Q:当使用nginx的proxy_pass代理一个后端的服务器(应用)时,如果后端服务器有多个虚拟主机,代理请求并不能产生正确的应答(nginx发起的请求中没有Host请求头),如何解决这个问题?
A:使用proxy_set_header指令设置主机头:

location / {
    proxy_pass http://my_app_upstream;
    proxy_set_header Host $host;
    # ...
}

怎么配置nginx rewrite,才不会引起浏览器url地址重定向?

来源:http://bbs.linuxtone.org/thread-10200-1-1.html

Q:Apache重写规则中的P参数代表为强制代理,即使用这个参数可以讲一个域名重写到另一个域名而不引起浏览器地址栏的url变化,Nginx中如何实现这一功能?
A:使用rewriteproxy_pass配合:

location ~ ^/frompath/ {
        rewrite ^/frompath/(.*)$ /topath/$1  break;
        proxy_pass  http://www.domain.com;
 }

假如请求为http://www.test.com/frompath/page.php,将被重写到http://www.domain.com/topath/page.php而不引起浏览器地址栏中url的变化

如何重写带参数的uri?

来源:无 
Q:如何使用Nginx重写/test.php?id=1&action=delete到/action.php?id=1&action=delete
A:参数保存在变量$query_string中,默认参数会跟在重写后的url后面,可以在新的规则后面加个问号:

set $query $query_string;
rewrite /test.php /action.php?$query?;

如果要求重写后的参数与重写前的参数不同,可以针对$query_string变量做正则匹配并配合set设置变量,这样就可以在重写规则中使用它们了。

猜你喜欢

转载自defenderhhhh.iteye.com/blog/2297678
FAQ