Ngnix 301重定向

时间:2015-10-17 00:05:16

标签: nginx

我正在使用nginx。 Nginx无法正确路由短地址。 对不起,我正在使用谷歌翻译。我的英语不好。

实施例

 location /a {
      return 301 http://www.example.com/abcd;
 }

或者

 location /a {
      rewrite ^(.*)$ http://www.example.com/abcd permanent;
 }

不能工作。

回复:重定向太多;

 >>> http://www.example.com/a

 > --------------------------------------------
 > 301 Moved Permanently
 > --------------------------------------------

 Status:    301 Moved Permanently
 Code:  301
 Server:    nginx
 Date:  Fri, 16 Oct 2015 23:46:00 GMT
 Content-Type:  text/html
 Content-Length:    178
 Connection:    close
 Location:  http://www.example.com/abcd

 >>> http://www.example.com/abcd

 > --------------------------------------------
 > 301 Moved Permanently
 > --------------------------------------------

 Status:    301 Moved Permanently
 Code:  301
 Server:    nginx
 Date:  Fri, 16 Oct 2015 23:46:00 GMT
 Content-Type:  text/html
 Content-Length:    178
 Connection:    close
 Location:  http://www.example.com/abcd

 >>> http://www.example.com/abcd

 > --------------------------------------------
 > 301 Moved Permanently
 > --------------------------------------------

 Status:    301 Moved Permanently
 Code:  301
 Server:    nginx
 Date:  Fri, 16 Oct 2015 23:46:01 GMT
 Content-Type:  text/html
 Content-Length:    178
 Connection:    close
 Location:  http://www.example.com/abcd

 >>> http://www.example.com/abcd

 > --------------------------------------------
 > 301 Moved Permanently
 > --------------------------------------------

 Status:    301 Moved Permanently
 Code:  301
 Server:    nginx
 Date:  Fri, 16 Oct 2015 23:46:01 GMT
 Content-Type:  text/html
 Content-Length:    178
 Connection:    close
 Location:  http://www.example.com/abcd

但是长地址成功了;

 location /what-can-i-do {
      rewrite ^(.*)$ http://www.example.com/please-help-me permanent;
 }

我的/etc/nginx/sites-available/example.com配置;

 server {
     server_name example.com www.example.com;
          if ($http_host = example.com) {
             return 301 http://www.example.com$request_uri;
          }
     listen 00.00.00.00:80;
     root /home/example/public_html;
     index index.html index.htm index.php;
     access_log /var/log/virtualmin/example.com_access_log;
     error_log /var/log/virtualmin/example.com_error_log;
     fastcgi_param GATEWAY_INTERFACE CGI/1.1;
     fastcgi_param SERVER_SOFTWARE nginx;
     fastcgi_param QUERY_STRING $query_string;
     fastcgi_param REQUEST_METHOD $request_method;
     fastcgi_param CONTENT_TYPE $content_type;
     fastcgi_param CONTENT_LENGTH $content_length;
     fastcgi_param SCRIPT_FILENAME /home/example/public_html$fastcgi_script_name;
     fastcgi_param SCRIPT_NAME $fastcgi_script_name;
     fastcgi_param REQUEST_URI $request_uri;
     fastcgi_param DOCUMENT_URI $document_uri;
     fastcgi_param DOCUMENT_ROOT /home/example/public_html;
     fastcgi_param SERVER_PROTOCOL $server_protocol;
     fastcgi_param REMOTE_ADDR $remote_addr;
     fastcgi_param REMOTE_PORT $remote_port;
     fastcgi_param SERVER_ADDR $server_addr;
     fastcgi_param SERVER_PORT $server_port;
     fastcgi_param SERVER_NAME $server_name;
     fastcgi_param HTTPS $https;

     location /a {
           return 301 http://www.example.com/abcd;
      }


     location /ajax {
        add_header "Access-Control-Allow-Origin" "*";
        try_files $uri $uri/ /index.php?$args;
     }

     set $no_cache "";
     if ($request_method = POST)
     {
         set $no_cache 1;
     }

     if ($request_method !~ ^(GET|POST|HEAD)$ ) {
        return 444;
     }

     server_tokens off;
     server_name_in_redirect off;

     location ~* .(ico|webp|css|js|ico|bmp|zip|woff)$
     {
         expires 30d;
         add_header Pragma public;
         add_header Cache-Control "public";
         try_files $uri /index.html;
     }

     location ~* \.(?:jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm|htc)$ {
         expires 1M;
         access_log off;
         add_header Cache-Control "public";
     }
     location ~* \.(?:css|js)$ {
         expires 1y;
         access_log off;
         add_header Cache-Control "public";
     }

     location / {
        try_files $uri $uri/ @rewrites;
     }    

     location @rewrites {
         if (!-e $request_filename)
         {
            rewrite ^/(.*)$ /index.php/$1 last;
            break;
         }
     }

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

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

     fastcgi_intercept_errors on;

     location ~ \.php {
           fastcgi_split_path_info ^(.+\.php)(/.+)$;

             fastcgi_pass_header Set-Cookie;
             fastcgi_pass_header Cookie;
             fastcgi_ignore_headers Cache-Control Expires Set-Cookie;

           fastcgi_param  PATH_INFO            $fastcgi_path_info;
           fastcgi_param  PATH_TRANSLATED      $document_root$fastcgi_path_info;

           fastcgi_param  QUERY_STRING         $query_string;
           fastcgi_param  REQUEST_METHOD       $request_method;
           fastcgi_param  CONTENT_TYPE         $content_type;
           fastcgi_param  CONTENT_LENGTH       $content_length;

           fastcgi_param  SCRIPT_NAME          $fastcgi_script_name;
           fastcgi_param  SCRIPT_FILENAME      $document_root$fastcgi_script_name;
           fastcgi_param  REQUEST_URI          $request_uri;
           fastcgi_param  DOCUMENT_URI         $document_uri;
           fastcgi_param  DOCUMENT_ROOT        $document_root;
           fastcgi_param  SERVER_PROTOCOL      $server_protocol;

           fastcgi_param  SERVER_SOFTWARE      nginx;

           fastcgi_param  REMOTE_ADDR          $remote_addr;
           fastcgi_param  REMOTE_PORT          $remote_port;
           fastcgi_param  SERVER_ADDR          $server_addr;
           fastcgi_param  SERVER_PORT          $server_port;
           fastcgi_param  SERVER_NAME          $server_name;

           fastcgi_index  index.php;
          fastcgi_pass unix:/var/run/php5-fpm.sock;
     }

     listen 00.00.00.00:00 default ssl;
     ssl_certificate /home/example/ssl.cert;
     ssl_certificate_key /home/example/ssl.key;
 }

1 个答案:

答案 0 :(得分:0)

/a后面的任何内容都会发送到与/abcd匹配的/a,前提是当前选项=>请求/a后,重定向到/a =>发生无限循环。

  location /a {
       return 301 http://www.example.com/abcd;
  }

将其更改为location = /a {,它的行为会有所不同。

TL;博士

位置块如下所示:

location optional_modifier location_match {

. . .

}

Here's a very informative page,引用optional_modifier

  
      
  • (none):如果不存在修饰符,则该位置将被解释为前缀匹配。这意味着给定的位置将与请求URI的开头匹配,以确定匹配。
  •   
  • =:如果使用等号,如果请求URI与给定的位置完全匹配,则此块将被视为匹配。
  •   
  • ...
  •   

因此,将其设置为=以进行直接匹配,或任何其他可用选项。