WordPress Nginx HTTPS反向代理配置

最近将博客进行了迁移,将博客运行于一台访问可能较慢配置较高的独服上,通过香港的VPS进行反向代理访问,提高访问速度,在配置WordPress反向代理时遇到了些问题。大体情况如下:

WordPress运行于A服务器,通过Nginx以http方式面向内网。B服务器位于同一内网,通过Nginx来反向代理A服务器的WordPress,同时以https方式面向外网。

此时,通过外网以https方式访问B机器反向代理的WordPress,会出现大量资源协议错误的设为http,且部分页面会出现循环跳转的情况。需要通过修改Nginx配置,来向WordPress正确传递外部是通过https访问这一信息,来修复这一问题。

A配置如下

server {
        listen    80;
        server_name a.example.com;
        root /path/to/expamle;
        index index.php index.html index.htm;

        location /{
                try_files $uri $uri/ /index.php?$args;
        }
        location ~ \.php$ {
                try_files $uri =404;
                include fastcgi.conf;
                fastcgi_param HTTPS $http_x_forwarded_ssl;
                fastcgi_param SERVER_NAME $host;
                fastcgi_param HTTP_HOST $host;
                fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        }
}

B配置如下

server {
        listen 80;
        server_name example.com;
        return  301     https://$server_name$request_uri;
}
server {
        listen 443 ssl http2;
        server_name example.com;
        ssl_certificate         example.crt;
        ssl_certificate_key     example.key;
        index index.html;
        location / {
                proxy_pass http://a.example.com;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection keep-alive;
                proxy_set_header Host $host;
                proxy_cache_bypass $http_upgrade;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header REMOTE-HOST $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header X-Forwarded-Ssl on;
        }
}

发表评论

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据