NGINX 将 https 重定向到 https + /location 获取循环重定向

时间:2021-07-03 17:00:42

标签: angular nginx nginx-config nginx-location

我正在尝试使用 nginx 服务器部署我的 angular 应用程序。我遇到的问题是,当我访问网站时,我需要重定向到特定位置,在这种情况下,我需要重定向到 mydomain.com/shop,当我进入该站点时,它会进入循环并开始输入 mydomain.com//shop/shop/shop/shop/shop......

这是我的代码。

  server {
    listen 443 ssl;

    ssl_certificate C:/nginx/ssl/cc.crt;
    ssl_certificate_key C:/nginx/ssl/pkc.key;

    server_name  mydomain.com www.mydomain.com;
    return 301 https://$host$request_uri/shop;
} 

server {
    listen       443 ssl;

    client_max_body_size 200M;

    ssl_certificate C:/nginx/ssl/cc.crt;
    ssl_certificate_key C:/nginx/ssl/pkc.key;

    server_name  mydomain.com www.mydomain.com;

    location / {
        root   html/myApp;       
        try_files $uri $uri/ /index.html;
    }

    location /api/ {
        client_max_body_size 200M;
        
        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_pass https://127.0.0.1:4000/;
        proxy_redirect off;
    }

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

我该如何解决?提前致谢

1 个答案:

答案 0 :(得分:1)

您有两个 constrained-categories 块具有相同的 serverlisten。第二个块作为重复项被忽略。

您需要删除第一个 server_name 块并将重定向规则放入第二个块中。

例如:

server