背景

在云服务器上搭建了wordpress,为了方便部署,采用了docker方式,使用了9102的端口号。然后通过nginx进行反向代理,使用域名访问。

问题

nginx配置很简单,直接使用了proxy_pass配置到docker容器的wordpress地址,如下:

server {
    listen 80;
    server_name www.我的域名.com;
    location / {
        proxy_pass http://10.0.4.6:9102;
        proxy_set_header Host $host;
        proxy_set_header X-Forward-For $remote_addr;
        proxy_redirect off;
    }
}

配置完成后,访问域名进行安装,配置,都正常。管理后台也可正常访问。但是想要进入博客首页时,出了问题,直接跳转到了http://10.0.4.6:9102。很明显,这个地址是无法访问也是不对的。
总结下问题:域名+/其他页面的形式是可以正常访问的,但是如果只有域名来访问首页就会出现301重定向问题。

Request URL: http://我的域名.com/
Request Method: GET
Status Code: 301 Moved Permanently (from disk cache)
Remote Address: 127.0.0.1:8889
Referrer Policy: strict-origin-when-cross-origin

Content-Length: 0
Content-Type: text/html; charset=UTF-8
Date: Thu, 19 Aug 2021 06:42:17 GMT
Location: http://10.0.4.5/
Server: nginx/1.21.1
X-Powered-By: PHP/7.4.22
X-Redirect-By: WordPress

解决方案:

多方查找,找到了方案,安装如下配置进行设置,即可解决。

server {
    listen       80;
    server_name  我的域名.com;
    
    set $node_port 9102;
    location / {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_set_header X-NginX-Proxy true;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_pass http://10.0.4.5:$node_port$request_uri;
        proxy_redirect off;
    }
}

标签: nginx, docker, wordpress, 301, 斜杆, 跳转, 首页

添加新评论