【问题标题】:Nginx HTTP & HTTPS Configs: Redirect SOME URLsNginx HTTP 和 HTTPS 配置:重定向一些 URL
【发布时间】:2014-09-18 13:05:43
【问题描述】:

我刚刚在 Nginx 上重新启动了一个 Wordpress 站点,通过 HTTP 提供内容。目前我们不打算将整个网站迁移到HTTPS,但是旧版网站的页面位于https://store.website.com

现在这些旧页面的新版本(例如,https://store.website.com/category/this-product.htmlhttps://store.website.com/that-product.html)位于 http://www.website.com/store/category/this-product/

我们在大约 10,000 个帖子中有很多指向 HTTPS 页面的链接,因此对于我们来说,与其更改帖子中的所有这些链接,不如简单地将这些 https://store.... 页面重定向到新的http 页面。

我有我们想要重定向的旧 HTTPS 页面的完整 URL 列表。我们仍在将一些产品加载到新网站(那里大约有 100 个),还需要再加载 900 个左右。因此,我们将对某些 URL(已加载的产品)使用 301 重定向,而对于仍需要加载的其余产品,我们将使用 302 重定向到顶级类别页面。加载这些新产品后,我们会将顶级类别页面的 302 重定向更改为指向永久主页的 301 重定向。

我只在 Nginx 中设置过 HTTP 服务器,所以这是我的问题:

  1. 我不确定创建 HTTPS 服务器是否会通过两种协议使内容可用,从而为每个帖子创建重复的内容
  2. 我与下面的配置有多接近来完成我所描述的?注意:下面是我的HTTP Nginx 配置,下面是HTTPS Nginx 配置。

*********HTTP NGINX 配置*********

    server {
            listen   80;

            access_log   /var/log/nginx/access.log;
            error_log    /var/log/nginx/error.log crit;

            root /var/www;
            index index.php;

            server_name website.com www.website.com;

            client_max_body_size 100M;

            location / {
                    try_files $uri $uri/ /index.php?q=$uri&$args;
                    #auth_basic "Admin Login";
                    #auth_basic_user_file /etc/nginx/pma_pass;
            }

            error_page 404 /404.html;

            # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
            location ~ \.php$ {
                    try_files $uri =404;
                    fastcgi_pass 127.0.0.1:9000;
                    # With php5-fpm:
                    #fastcgi_pass unix:/var/run/php5-fpm.sock;
                    fastcgi_index index.php;
                    include fastcgi_params;
                    fastcgi_read_timeout 60;
            }
            # BEGIN W3TC CDN
            location ~ \.(ttf|ttc|otf|eot|woff|font.css)$ {
               add_header Access-Control-Allow-Origin "*";
            }
            # END W3TC CDN
            #Yoast sitemap
            location ~ ([^/]*)sitemap(.*)\.x(m|s)l$ {
                    rewrite ^/sitemap\.xml$ /sitemap_index.xml permanent;
                    rewrite ^/([a-z]+)?-?sitemap\.xsl$ /index.php?xsl=$1 last;
                    rewrite ^/sitemap_index\.xml$ /index.php?sitemap=1 last;
                    rewrite ^/([^/]+?)-sitemap([0-9]+)?\.xml$ /index.php?sitemap=$1&sitemap_n=$2 last;
                    access_log off;
            }
            # Example of the style of single-page redirects we are currently using
            location = /this-page/ { return 301 /store/category/that-page/; }
    }

********HTTPS NGINX 配置********

    server {
           listen 443;
           server_name store.website.com;

           root /var/www;
           index index.php;

           ssl on;
           ssl_certificate /etc/nginx/ssl/server.crt;
           ssl_certificate_key /etc/nginx/ssl/server.key;

           ssl_session_timeout 10m;

           ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2;
           ssl_ciphers "HIGH:!aNULL:!MD5 or HIGH:!aNULL:!MD5:!3DES";
           ssl_prefer_server_ciphers on;

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

感谢您的意见!

【问题讨论】:

    标签: wordpress http redirect nginx https


    【解决方案1】:

    您可以考虑创建一个新的配置来侦听您的旧站点 URL,并创建一个重写规则以重定向到新站点。

    http://nginx.org/en/docs/http/ngx_http_rewrite_module.html

    更新 1:

    server {
           listen 443;
           server_name store.website.com;
    
           ssl on;
           ssl_certificate /etc/nginx/ssl/server.crt;
           ssl_certificate_key /etc/nginx/ssl/server.key;
    
           ssl_session_timeout 10m;
    
           ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2;
           ssl_ciphers "HIGH:!aNULL:!MD5 or HIGH:!aNULL:!MD5:!3DES";
           ssl_prefer_server_ciphers on;
    
           # To rewrite URLs like https://store.website.com/category/this-product.html
           rewrite ^/category/(.*).html$  http://www.website.com/store/category/$1 permanent;
    
           # To rewrite URLs like https://store.website.com/that-product.html
           rewrite ^/(.*).html$  http://www.website.com/store/category/$1 permanent;
    
           # To rewrite URLs like https://store.website.com/abcasc/q343f/3f3/f3
           rewrite ^(.*) http://www.website.com$1 permanent; 
    }
    

    未测试。但请随意尝试。

    【讨论】:

    • 嗯,我想了这么多,除非我弄错了,这就是我在上面试图弄清楚的。这甚至不能回答我的问题。
    • 据我所知,您上面的配置并未表明在 store.website.com 上进行任何监听。
    • 好收获。已编辑。您是否注意到我提到的关于重复内容和配置对我所描述的内容的一般适用性的两个问题?
    • 是的,我做到了。如果您在 HTTPS 配置中使用 rewrite 指令,我认为它不会创建重复的内容,因为它会重定向到您想要的位置。我建议您将 store.website.com 中的所有 url 重写为 www.website.com。
    • 服务器 { 听 443; server_name store.website.com;重写 ^(.*) http://www.website.com$1 永久; }
    猜你喜欢
    • 2020-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-08
    • 1970-01-01
    • 2018-04-24
    • 1970-01-01
    • 2011-03-29
    相关资源
    最近更新 更多