NGinx 无法重定向到 /admin 或任何其他页面,即使它们在位置块中定义

NGinx 无法重定向到 /admin 或任何其他页面,即使它们在位置块中定义

所以我创建了这个 Nginx 配置文件,每次我尝试使用 URL/admin 或 URL/swagger 打开页面时,我仍然会被重定向到 /URL/home,这很奇怪。我已经尝试了很多方法,但都没有用。如果有人能检查我的配置并指出我这里缺少什么,那将非常有帮助。

server {
    listen [::]:443 ssl ipv6only=on; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/app.abc.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/app.abc.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
    keepalive_timeout 5;

    root /home/ubuntu/abc-frontend/dist;
    # Add index.php to the list if you are using PHP
    index index.html index.htm;

    server_name app.abc.com;
    client_max_body_size 100M;

    location / {
        try_files $uri $uri/ /index.html;
        location ~*\.(?:ico|css|js|json|gif|jpe?g|png)$ {
                expires 15d;
                add_header Pragma public;
                add_header Cache-Control "public";
                add_header Vary "Accept-Encoding";
        }
    }

    location /api/ {
        alias /home/ubuntu/abc;
        proxy_pass http://unix:///tmp/abc.sock;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Url-Scheme $scheme;
        proxy_set_header  X-Forwarded-Proto https;
    }

    location /admin/?(.*) {
        alias /home/ubuntu/abc;
        proxy_pass http://unix:///tmp/abc.sock;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Url-Scheme $scheme;
        proxy_set_header  X-Forwarded-Proto https;
    }

    location /maintenance_admin/ {
        alias /home/ubuntu/abc;
        #proxy_pass http://abc;
        proxy_pass http://unix:///tmp/abc.sock;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Url-Scheme $scheme;
        proxy_set_header  X-Forwarded-Proto https;
    }

    location /swagger/ {
        alias /home/ubuntu/abc;
        #proxy_pass http://abc;
        proxy_pass http://unix:///tmp/abc.sock;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Url-Scheme $scheme;
        proxy_set_header  X-Forwarded-Proto https;
    }

    location /.well-known/acme-challenge/ {
        root /var/www/certbot;
    }

    # ignore cache frontend
    location ~* (service-worker\.js)$ {
        add_header 'Cache-Control' 'no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0';
        expires off;
        proxy_no_cache 1;
    }

    gzip on;
    gzip_disable "msie6";
    gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript;
}

server {
    listen         80;
    server_name    app.abc.com;
    return         301 https://$host$request_uri;

答案1

你的位置块有问题:请尝试使用以下内容替代:

如果请求的 URi 与给定的位置完全匹配,则 = 此块将被视为匹配。

location = / {
    try_files $uri $uri/ /index.html;
    location ~*\.(?:ico|css|js|json|gif|jpe?g|png)$ {
            expires 15d;
            add_header Pragma public;
            add_header Cache-Control "public";
            add_header Vary "Accept-Encoding";
    }
}

我认为那可以解决您的问题,无论如何,如果没有,您可以尝试在块的开头添加〜,如下所示:

location ~ api/ {
    alias /home/ubuntu/abc;
    proxy_pass http://unix:///tmp/abc.sock;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Url-Scheme $scheme;
    proxy_set_header  X-Forwarded-Proto https;
}

你也可以查看这篇文章以获得更好的理解 https://www.digitalocean.com/community/tutorials/understanding-nginx-server-and-location-block-selection-algorithms

相关内容