Nginx不照顾没有反斜杠的网址

时间:2018-10-18 07:23:08

标签: nginx web-deployment

我的nginx配置遇到一个奇怪的问题。如果我在URL末尾打了反斜杠(/),则没有加载我的Web应用程序,而如果我反斜杠(/),则一切正常。例如,如果我点击https://qc.example.com/testhtml,则该网址会自动重写为https://qc.example.com:8080/testhtml/,并且页面上没有任何内容。但是,如果我添加反斜杠https://qc.example.com/testhtml/,则网页将成功呈现。

下面是我的conf文件。

server {
    listen       8080;
    error_log  /app/log/nginx_error.log;
    root /app/testhtml;
    index index.html;
    ssl on;
    ssl_certificate /app/ssl/qc.example.com.pem;
    ssl_certificate_key /app/ssl/privkey.pem;
    ssl_session_cache    shared:SSL:1m;
    ssl_session_timeout  5m;

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

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

1 个答案:

答案 0 :(得分:0)

尝试一下:

重写^(。* [^ /])$ $ 1 /永久;

它将在URL末尾添加反斜杠


    server {
        listen       8080;

        rewrite ^(.*[^/])$ $1/ permanent;

        error_log  /app/log/nginx_error.log;
        root /app/testhtml;
        index index.html;
        ssl on;
        ssl_certificate /app/ssl/qc.example.com.pem;
        ssl_certificate_key /app/ssl/privkey.pem;
        ssl_session_cache    shared:SSL:1m;
        ssl_session_timeout  5m;

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

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