【问题标题】:NGINX https://www.example.com/index.php to https://www.example.com/ redirect how?NGINX https://www.example.com/index.php 到 https://www.example.com/ 重定向如何?
【发布时间】:2014-09-22 12:28:50
【问题描述】:

我有这个问题,我以为我已经解决了,但我没能解决它。我要重定向:

----------------------------------------------------------------------------
|                   FROM                  |              TO                |
----------------------------------------------------------------------------
|     https://www.wknet.se/index.php      |      https://www.wknet.se/     |
----------------------------------------------------------------------------

我已尝试添加:

location = /index.php {
      return 301 https://www.wknet.se;
}

但是它在浏览器中变成了一个无限循环的重定向错误。这是我的配置:

server {
   listen 80 default_server;
   listen [::]:80 default_server ipv6only=on;
   server_name wknet.se www.wknet.se;

   add_header Strict-Transport-Security max-age=15768000;
   return 301 https://www.wknet.se$request_uri;
}

server {
   listen 443 ssl;
   server_name wknet.se;

   ssl_certificate /etc/nginx/ssl/SSL.crt;
   ssl_certificate_key /etc/nginx/ssl/KEY.key;

   ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2;
   ssl_ciphers ALL:!aNULL:!ADH:!eNULL:!LOW:!EXP:RC4+RSA:+HIGH:+MEDIUM;
   ssl_prefer_server_ciphers on;

   return 301 https://www.wknet.se$request_uri;
}

server {
   listen 443 ssl;
   server_name www.wknet.se;

   root /var/www/wknet.se/html;
   index index.php index.html index.htm;

   ssl_certificate /etc/nginx/ssl/SSL.crt;
   ssl_certificate_key /etc/nginx/ssl/KEY.key;

   ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2;
   ssl_ciphers ALL:!aNULL:!ADH:!eNULL:!LOW:!EXP:RC4+RSA:+HIGH:+MEDIUM;
   ssl_prefer_server_ciphers on;

   error_page 404 /404.html;
   error_page 500 502 503 504 /50x.html;

   location / {
      try_files $uri $uri/ =404;
   }

   location = /50x.html {
      root /usr/share/nginx/html;
   }

   location ~ \.php$ {
      try_files $uri =404;
      fastcgi_split_path_info ^(.+\.php)(/.+)$;
      fastcgi_pass unix:/var/run/php5-fpm.sock;
      fastcgi_index index.php;
      include fastcgi_params;
   }

   location = /index {
      return 301 https://www.wknet.se;
   }

   location ~ /\.ht {
      deny all;
   }

   location = /favicon.ico {
      log_not_found off;
      access_log off;
   }

   location = /robots.txt {
      allow all;
      log_not_found off;
      access_log off;
   }

   location ~ /\. { 
      deny all; 
      error_log off; 
      log_not_found off; 
   }

   location ~* .(jpg|jpeg|png|gif|ico|css|js)$ {
      log_not_found off;
      expires 365d;
   }

   location ~* ^.+\.(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|rss|atom|jpg|jpeg|png|gif|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$ {
      access_log off;
      log_not_found off;
      expires max;
      add_header Pragma public;
      add_header Cache-Control "public, must-revalidate, proxy-revalidate";
   }

   location ~* \.(7z|ai|class|css|csv|ejs|eps|flv|html?|jar|jpe?g|js|json|lzh|m4a|m4v|mov|mp3|pdf|pict|pls|ps|psd|swf|tiff?|txt|webp)$ {
      access_log off; 
      log_not_found off;
      expires max;
      add_header Pragma public;
      add_header Cache-Control "public, must-revalidate, proxy-revalidate";
   }
 }

我已经用谷歌搜索并测试了我在 Internet 上找到的一些代码,但没有任何帮助。我错过了什么?

【问题讨论】:

    标签: redirect ssl nginx


    【解决方案1】:

    您最好的选择是在您的实际应用程序中删除所有指向 index.php 的链接,并只处理任何特定请求,包括 index.php 而无需尝试更改此类请求。

    在应用程序中处理后应该很少甚至没有,因为搜索引擎会保存没有index.php 的链接。

    不确定您当前的位置...

       location = /index {
          return 301 https://www.wknet.se;
       }
    

    ... 位于事物方案中(当然与您的 index.php 问题无关),除非您特别想重定向 index 文件夹,否则它可能应该被删除,因为它可能会导致您出现问题。

    ** 编辑 **

    又被捅了一刀。未经测试,但可能会打破循环。 尝试在您的原始配置中将其添加到 error_page 500 502 503 504 /50x.html; 下方

       # We need to check if the user has specifically requested "index.php"
       #    and only redirect if they have ($request_uri variable). 
       #    Internal redirects ($uri variable) should be left alone to avoid a redirect loop.
       if ( $request_uri ~ ^(/index\.php)$ ) {
            return 301 https://www.wknet.se;
        }
    

    【讨论】:

    • 它不起作用,我添加了建议但我不断收到重定向的无限循环错误。看看 --> https://www.wknet.se/。我就是想不通。如何进行嵌套?
    • 对不起...这总是会导致重定向循环。我会修改答案。
    • 就如何摆脱死循环没有其他解决方案吗? :(
    • @damircalusic 见编辑部分
    • 我测试了添加你的编辑,但我得到了一个 --> 未知指令 "if($request_uri". :/
    猜你喜欢
    • 2017-10-26
    • 2011-07-06
    • 2011-08-12
    • 1970-01-01
    • 2015-09-25
    • 1970-01-01
    • 2019-11-11
    • 2017-07-27
    • 1970-01-01
    相关资源
    最近更新 更多