Nginx 服务器重定向循环

发布于 2025-01-11 00:28:14 字数 3884 浏览 0 评论 0原文

今晚部署后,我的 nginx 服务器不断收到 301 重定向到相同的 URI,因此浏览器不会加载该网站。它是一个 docker 容器内的节点应用程序,总共有 4 个容器:app、wordpress、redis、nginx。我很确定我没有更改 docker 项目中 nginx 配置中的任何内容,只是更改了子项目(src-folder)。我什至将所有更改恢复到之前的状态,但问题仍然存在。

普通 URL 和 Wordpress 域都不会加载,它总是 301 重定向到根目录。

配置应将所有流量从非 SSL 301 重定向到 SSL 并呈现页面。

我的配置如下所示:

upstream node {
    server app:3000;
}

server {
        listen 80;
        server_name example.com www.example.com;

        return 301 https://example.com;
}

server {
    listen 80;
    server_name wordpress.example.com;

    return 301 https://example.com;
}

server {
        listen 443;
        server_name www.example.com;
    ssl_certificate /etc/nginx/ssl/example.com.cert;
    ssl_certificate_key /etc/nginx/ssl/example.com.key;

        return 301 https://example.com;
}
server {
    listen 443 default ssl;
    server_name example.com;
    root /var/www/html/project;

    ssl_certificate /etc/nginx/ssl/example.com.cert;
    ssl_certificate_key /etc/nginx/ssl/example.com.key;

    set $isopen 0;


    location / {
        proxy_pass http://node;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header Accept-Encoding "";
        proxy_set_header Proxy "";
        expires off;
    }
    location /lp/ {
        proxy_ssl_server_name on;
        proxy_pass https://sub.example.com/folder/;
    }
}

server {
    listen 443 ssl;
    server_name wordpress.example.com;
    ssl_certificate /etc/nginx/ssl/wordpress.example.com.cert;
    ssl_certificate_key /etc/nginx/ssl/wordpress.example.com.key;

    error_page 397 https://$host:$server_port$request_uri;

    set $isopen 0;

    if ($request_uri ~ '^/wp-json/wp/v2/.*') {
        set $isopen 1;
    }
    if ($request_uri ~ '^/wp-includes/.*') {
        set $isopen 1;
    }
    if ($request_uri ~ '^/wp-content/.*') {
        set $isopen 1;
    }
    if ($request_uri ~ '^/wp-admin/.*') {
        set $isopen 1;
    }
    if ($request_uri ~ '^/wp-login.php') {
        set $isopen 1;
    }
    if ($request_uri ~ '^/wp-admin/.*\.(php|css|js)') {
        set $isopen 1;
    }
    if ($isopen !~ 1) {
        return 301 https://$server_name/wp-admin/;
    }

    location / {
            proxy_pass https://wordpress;
        proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
            proxy_set_header Accept-Encoding "";
            proxy_set_header Proxy "";
            expires off;
       }
}

我的 docker 容器侦听以下端口:

Containers statuses 
              Name                            Command               State                      Ports                    
------------------------------------------------------------------------------------------------------------------------
project_app_1         bash -c npm install && npm ...   Up                                                  
project_redis_1       docker-entrypoint.sh redis ...   Up      6379/tcp                                    
project_web-srv_1     nginx -g daemon off;             Up      0.0.0.0:44301->443/tcp, 0.0.0.0:8080->80/tcp
project_wordpress_1   docker-entrypoint.sh apach ...   Up      443/tcp, 80/tcp                             

 Network status 
Network name                    Network subnet      Network gateway
project_network    172.100.112.0/24    172.100.112.1

Container name                      Container IPv4 address
project_wordpress_1    172.100.112.50/24
project_web-srv_1      172.100.112.30/24
project_app_1          172.100.112.40/24
project_redis_1        172.100.112.20/24

希望有人可以帮助我。提前致谢。

after a deployment tonight my nginx server keeps getting 301 redirecting to the same URI and thus browsers won't load the site. It's a node app within a docker container with 4 containers in total: app, wordpress, redis, nginx. I am pretty sure that I didn't change anything in the nginx config within the docker project, just changed the sub project (src-folder). I even reverted all my changes back to what I had before but the issue stays.

Neither the normal URL nor the Wordpress domain is loading, it will always 301 redirect to the root.

The configuration should 301 redirect all traffic from non-SSL to SSL and render the page.

My config looks like this:

upstream node {
    server app:3000;
}

server {
        listen 80;
        server_name example.com www.example.com;

        return 301 https://example.com;
}

server {
    listen 80;
    server_name wordpress.example.com;

    return 301 https://example.com;
}

server {
        listen 443;
        server_name www.example.com;
    ssl_certificate /etc/nginx/ssl/example.com.cert;
    ssl_certificate_key /etc/nginx/ssl/example.com.key;

        return 301 https://example.com;
}
server {
    listen 443 default ssl;
    server_name example.com;
    root /var/www/html/project;

    ssl_certificate /etc/nginx/ssl/example.com.cert;
    ssl_certificate_key /etc/nginx/ssl/example.com.key;

    set $isopen 0;


    location / {
        proxy_pass http://node;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header Accept-Encoding "";
        proxy_set_header Proxy "";
        expires off;
    }
    location /lp/ {
        proxy_ssl_server_name on;
        proxy_pass https://sub.example.com/folder/;
    }
}

server {
    listen 443 ssl;
    server_name wordpress.example.com;
    ssl_certificate /etc/nginx/ssl/wordpress.example.com.cert;
    ssl_certificate_key /etc/nginx/ssl/wordpress.example.com.key;

    error_page 397 https://$host:$server_port$request_uri;

    set $isopen 0;

    if ($request_uri ~ '^/wp-json/wp/v2/.*') {
        set $isopen 1;
    }
    if ($request_uri ~ '^/wp-includes/.*') {
        set $isopen 1;
    }
    if ($request_uri ~ '^/wp-content/.*') {
        set $isopen 1;
    }
    if ($request_uri ~ '^/wp-admin/.*') {
        set $isopen 1;
    }
    if ($request_uri ~ '^/wp-login.php') {
        set $isopen 1;
    }
    if ($request_uri ~ '^/wp-admin/.*\.(php|css|js)') {
        set $isopen 1;
    }
    if ($isopen !~ 1) {
        return 301 https://$server_name/wp-admin/;
    }

    location / {
            proxy_pass https://wordpress;
        proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
            proxy_set_header Accept-Encoding "";
            proxy_set_header Proxy "";
            expires off;
       }
}

My docker containers listen to the following ports:

Containers statuses 
              Name                            Command               State                      Ports                    
------------------------------------------------------------------------------------------------------------------------
project_app_1         bash -c npm install && npm ...   Up                                                  
project_redis_1       docker-entrypoint.sh redis ...   Up      6379/tcp                                    
project_web-srv_1     nginx -g daemon off;             Up      0.0.0.0:44301->443/tcp, 0.0.0.0:8080->80/tcp
project_wordpress_1   docker-entrypoint.sh apach ...   Up      443/tcp, 80/tcp                             

 Network status 
Network name                    Network subnet      Network gateway
project_network    172.100.112.0/24    172.100.112.1

Container name                      Container IPv4 address
project_wordpress_1    172.100.112.50/24
project_web-srv_1      172.100.112.30/24
project_app_1          172.100.112.40/24
project_redis_1        172.100.112.20/24

Hope somebody can help me. Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文