我正在尝试将/index.html 重写为/以用于 SEO 目的(愚蠢的搜索引擎将 index.html 与/混淆并惩罚重复的内容)——也是为了协调网络分析数据。
我已经尝试了在 stackoverflow、nginx 文档等上找到的所有解决方案,但都没有成功。我想我一定有其他一些配置问题或其他明显的问题。这是我的第一个 nginx 安装——用于 Apache 和 IIS!
这是我的 default.conf:
server {
listen 80;
server_name web.local;
#charset koi8-r;
#access_log /var/log/nginx/log/host.access.log main;
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /var/www/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
这是我的 virtual.conf (注释掉的部分是我最近的尝试——当你尝试访问 www.domain.com/index.html 时,当取消注释它会给出 301 Moved Permanently 错误):
server {
listen 80;
server_name www.domain.com;
location / {
root /var/www/html/domain.com;
index index.html;
#if ($request_uri = /index.html) {
# rewrite ^ http://www.domain.com permanent;
#}
}
}
server {
listen 80;
server_name domain.com;
rewrite ^/(.*) http://www.domain.com/$1 permanent;
}
cobaco 解决方案的 HTTP 响应 header :
URL:
http://www.domain.com
http/1.1 301 moved permanently
server: nginx/1.2.8
date: Thu, 16 May 2013 01:42:58 GMT
content-type: text/html
content-length: 184
connection: keep-alive
location: http://domain.com/
Redirecting URL:
http://domain.com/
http/1.1 301 moved permanently
server: nginx/1.2.8
date: Thu, 16 May 2013 01:42:58 GMT
content-type: text/html
content-length: 184
connection: keep-alive
location: http://www.domain.com/
我认为这一行可能会导致问题:“location =/index.html {return 301 $scheme://domain.com/;}”所以我添加了 www.在“scheme://”之后——让我知道这是否是一件坏事!这导致了以下 HTTP 响应 header :
URL:
http://www.domain.com
http/1.1 301 moved permanently
server: nginx/1.2.8
date: Thu, 16 May 2013 01:42:58 GMT
content-type: text/html
content-length: 184
connection: keep-alive
location: http://www.domain.com/
Redirecting URL:
http://www.domain.com/
http/1.1 301 moved permanently
server: nginx/1.2.8
date: Thu, 16 May 2013 01:42:58 GMT
content-type: text/html
content-length: 184
connection: keep-alive
location: http://www.domain.com/
经过一些修补后,以下配置完成了我想要它做的事情,但由于 if 语句而不理想。有什么建议?
server {
server_name www.domain.com;
root /var/www/html/domain.com;
index index.html;
if ($request_uri = /index.html) {
return 301 http://www.domain.com/;
}
#location = /index.html {
# return 301 $scheme://www.domain.com/;
#}
}
server {
listen 80;
server_name domain.com;
return 301 $scheme://www.domain.com$request_uri;
}
最佳答案
您的最终解决方案完全没问题。if
指令只有在 location
内才是邪恶的堵塞。另外你只有一个 return
if
中的指令堵塞。我看不出有什么问题。引用:http://wiki.nginx.org/IfIsEvil
cobaco 解决方案中的无限重定向循环是因为
index index.html;
触发另一轮位置匹配。所以nginx会陷入
location = /index.html
再次重定向到 http://www.domain.com/
.
关于nginx/index.html 到/重写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16535209/