【问题标题】:wordpress nginx ssl redirect loopwordpress nginx ssl重定向循环
【发布时间】:2015-02-26 04:39:14
【问题描述】:

所以我设置了一个 nginx 服务器并安装了 wordpress 和 SSL。

该站点在 http 和 https 上都运行良好,但是当我尝试通过 nginx 的服务器块将 http 重定向到 https 时,http 和 https 都会导致无限的重定向循环。

这是我的服务器块

server {
    listen 80;
    return         301 $server_name$request_uri;
    listen 443 ssl spdy;
    root /var/www/wordpress;
    index index.php index.html index.htm;
    server_name www.example.com;
    ssl_session_cache shared:SSL:20m;
    ssl_session_timeout 10m;
    spdy_headers_comp 6;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_certificate /etc/ssl/certs/www.example.com.certchain.crt;
    ssl_certificate_key /etc/ssl/private/www.example.com.key;
    add_header Strict-Transport-Security "max-age=31536000; includeSubDomains";
    add_header        Alternate-Protocol  443:npn-spdy/2;
    proxy_set_header X-Forwarded-Proto https;

    access_log   /var/log/nginx/example.com.access.log;
    error_log    /var/log/nginx/example.com.error.log;

    error_page 404 /404.html;

    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
            root /usr/share/nginx/html;
    }


location / {
            proxy_set_header        X-Forwarded-Proto $scheme;
            # try_files $uri $uri/ =404;
            try_files $uri $uri/ /index.php?q=$uri&$args;
    if ($http_referer ~* (buttons-for-website.com)) { return 444; }
    if ($http_referer ~* (semalt.com)) { return 444; }
    }


    location ~ \.(hh|php)$ {
    proxy_set_header X-Forwarded-Proto https;
    proxy_set_header X-Forwarded-Ssl on;
        fastcgi_keep_conn on;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include        fastcgi_params;
        fastcgi_cache_bypass $skip_cache;
        fastcgi_no_cache $skip_cache;
    fastcgi_cache microcache;
    fastcgi_cache_valid 200 60m;

 }

  location ~ \.php$ {

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

fastcgi_cache 微缓存; fastcgi_cache_valid 200 60m;

 }


 # Cache Static Files For As Long As Possible
location ~*
 \.(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|css|rss|atom|js|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|$
{
  access_log off;
  log_not_found off;
  expires max;
   }
# Security Settings For Better Privacy Deny Hidden Files
location ~ /\. {
deny all;
access_log off;
log_not_found off;
}
# Return 403 Forbidden For readme.(txt|html) or license.(txt|html)
if ($request_uri ~* "^.+(readme|license)\.(txt|html)$") {
 return 403;
}
# Disallow PHP In Upload Folder
location /wp-content/uploads/ {
location ~ \.php$ {
deny all;
}
}
}

我非常感谢任何人的帮助。我在第 3 行注释掉了“返回 301”,谷歌同时索引了同一页面的 http 和 https 版本,并取消了我的大部分页面的索引,并降低了几个关键字的排名。

提前致谢!

【问题讨论】:

    标签: wordpress ssl nginx


    【解决方案1】:

    尝试分离非 ssl 服务器块,这样你就有了这个

    server {
        listen 80;
        return         301 https://$host$request_uri;
    }
    
    server {
        listen 443 ssl spdy;
        root /var/www/wordpress;
    ....
    

    【讨论】:

    • 感谢您的评论布赖恩。是的,它现在正确重定向。当我尝试访问example.comexample.com 的站点时,它完美地重定向到example.com,但是当我输入example.com 时,它没有重定向到example.com,而是将页面加载到example.com 本身。跨度>
    • 这是我当前代码的样子:'server { listen 80; server_name example.com;返回 301example.com$request_uri; } 服务器 { 听 443 ssl spdy;根 /var/www/wordpress; server_name www.example.com;索引 index.php index.html index.htm; ....}'
    • 对于 www。重定向你需要再添加一个服务器块 server { listen 443 ssl spdy;服务器名称_example.com;返回 301$host$requesturi; } 并添加一个 server_name www.example.com;指令到您的其他 443 服务器块或将 default_server 添加到该服务器块中的侦听选项。
    猜你喜欢
    • 2013-11-27
    • 2015-02-15
    • 1970-01-01
    • 2019-08-31
    • 2015-12-09
    • 2014-06-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-19
    相关资源
    最近更新 更多