在 URL 末尾添加斜线(图片、css、js 和 xml 除外),并保留端口

在 URL 末尾添加斜线(图片、css、js 和 xml 除外),并保留端口

我试图在我的网站上所有 URL 末尾添加斜线,但带扩展名的文件(所有图像类型、css、js、xml)除外,同时保留我本地环境的端口。

这是我正在寻找的行为:

In production:

 https://testsite.com/page/something →  https://testsite.com/page/something/

 https://testsite.com/page/something/img.jpg →  https://testsite.com/page/something/img.jpg

 https://testsite.com/page/something?param=23 →  https://testsite.com/page/something?param=23/

Local environment

localhost:1080/page/something → localhost:1080/page/something/

localhost:1080/page/something/img.jpg → localhost:1080/page/something/img.jpg

localhost:1080/page/something?param=23 → localhost:1080/page/something?param=23/

我一直尝试通过添加来实现这一点:

if ($request_uri ~ ^(.*)(\/(?!jpg|gif|png|jpeg|xml|css|js)([^.\/])+)$) {
            try_files $uri $uri/ /index.php?$query_string/;
        }

在 - 的里面

location / 

声明,对于端口,我写了

port_in_redirect off;

指示。

完整配置如下:

server {
    listen 80;
    server_name _;
    root /var/www/html/public;

    index index.html index.htm index.php;
    charset utf-8;

    client_max_body_size 100m;
    
    port_in_redirect off;

    access_log  /var/log/nginx/access.log  main;
    error_log /var/log/nginx/error.log;

    gzip on;
    gzip_types text/plain text/css application/javascript text/javascript image/svg+xml image/png image/gif application/font-woff application/xml application/json application/octet-stream;
    gzip_proxied any;
    gzip_vary on;

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt { access_log off; log_not_found off; }

    location / {
        if ($request_uri ~ ^(.*)(\/(?!jpg|gif|png|jpeg|xml|css|js)([^.\/])+)$) {
            try_files $uri $uri/ /index.php?$query_string/;
        }
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
        fastcgi_pass web:9000;
        fastcgi_read_timeout 300;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
    }

}

但是现在,网站似乎根本无法加载。我想知道最好的方法是什么。提前谢谢。

相关内容