现在的位置: 首页 > IT运维 > 正文

Nginx访问域名或目录无斜杠的解决方法续

2012年03月31日 IT运维 ⁄ 共 1612字 暂无评论 ⁄ 被围观 0+

Nginx访问域名或目录链接后无/(反斜杠)无法访问的解决方法中说到,因为域名或目录后无斜杠,导致用户无法打开网页的故障,当时的解决方法是在nginx.conf里修改,让nginx后缀中的"/"斜杠自动补齐:

if (-d $request_filename)
{
rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;
}

现在要介绍个更好的方法,在nginx.conf直接设置:

optimize_server_names off;#优化服务器名称:关 (默认开启)
server_name_in_redirect off;#服务器名称重定向:关(默认开启)

官方参数说明:

optimize_server_names (优化服务器名称)
syntax: optimize_server_names [ on|off ]
default: optimize_server_names on
context: http, server
Directive activates or deactivates optimization of host name checks for name-based virtual servers.
In particular, the check influences the name of the host used in redirects. If optimization is on, and all name-based servers listening on one address:port pair have identical configuration, then names are not checked during request execution and redirects use first server name.
If redirect must use host name passed by the client, then the optimization must be turned off.
Note: this directive is deprecated in nginx 0.7.x, use server_name_in_redirect instead.
 
server_name_in_redirect (服务器名称重定向)
syntax: server_name_in_redirect on|off
default: server_name_in_redirect on
context: http, server, location
If server_name_in_redirect is on, then Nginx will use the first value of the server_name directive for redirects. If server_name_in_redirect is off, then nginx will use the requested Host header.
Note: for Location headers coming from an upstream proxy (via proxy_pass for example) this may not be the only directive you need. In fact, it seems to be ignored a lot of the time. If you are seeing the upstream's server name come through and not be rewritten, you will need to use proxy_redirect to rewrite the upstream's provided hostname to what you want. Something like proxy_redirect http://some.upstream.url/ / - you will want to rewrite it to a / relative path.

给我留言

您必须 [ 登录 ] 才能发表留言!

×
#