nginx /index.html到/重写

前端之家收集整理的这篇文章主要介绍了nginx /index.html到/重写前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我正在尝试将/index.html重写为/用于搜索引擎优化目的(愚蠢的搜索引擎混淆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响应标头:

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响应标头:

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块中有一个return指令.我没有看到任何错误.参考:http://wiki.nginx.org/IfIsEvil

cobaco解决方案中的无限重定向循环是因为

  index  index.html;

触发另一轮位置匹配.因此,在重定向到http://www.domain.com/之后,Nginx将再次被困在location = /index.html中.

原文链接:https://www.f2er.com/nginx/434532.html

猜你在找的Nginx相关文章