Nginx抛出404请求,而不是执行try_files

时间:2017-03-20 05:41:05

标签: nginx

我的nginx配置文件中有以下块:

location /cat {
   try_files $uri $uri/ /index.php?url=$uri;
}

当我尝试以下任何网址时,这会按预期工作:

http://example.com/cat/test
http://example.com/cat/test/test
http://example.com/cat/t

然而,以下情况并非如此。我没有重定向,而是收到404错误。

http://example.com/cat/test/
http://example.com/cat/test/test/
http://example.com/cat/t/

我对nginx不是很有经验,所以让我知道可能需要哪些进一步的信息。这让我非常难过。

编辑:我确实碰到过这个,但我宁愿避免使用重写(为了提高效率),而且这似乎是一个不必要的创可贴: https://serverfault.com/questions/755646/nginx-use-try-files-to-serve-file-instead-of-directory

编辑2:这是完整的配置

server {
    listen 64.40.99.86:80;

    server_name example.com;
    server_name www.example.com;

    client_max_body_size 128m;

    root "/var/www/vhosts/example.com/httpdocs";
    access_log "/var/www/vhosts/system/example.com/logs/proxy_access_log";
    error_log "/var/www/vhosts/system/example.com/logs/proxy_error_log";

    if ($host ~* ^example.com$) {
        rewrite ^(.*)$ http://www.example.com$1 permanent;
    }

    location / {
        proxy_pass http://64.40.99.86:7080;
        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-Accel-Internal /internal-nginx-static-location;
        access_log off;
    }

    location /internal-nginx-static-location/ {
        alias /var/www/vhosts/example.com/httpdocs/;
        add_header X-Powered-By PleskLin;
        internal;
    }

    location ~ ^/~(.+?)(/.*?\.php)(/.*)?$ {
        alias /var/www/vhosts/example.com/web_users/$1/$2;
        fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
        fastcgi_param PATH_INFO $fastcgi_path_info;
        fastcgi_pass "unix:///var/www/vhosts/system/example.com/php-fpm.sock";
        include /etc/nginx/fastcgi.conf;
    }

    location ~ ^/~(.+?)(/.*)?$ {
        proxy_pass http://64.40.99.86:7080;
        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-Accel-Internal /internal-nginx-static-location;
        access_log off;
    }

    location ~ \.php(/.*)?$ {
        fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
        fastcgi_param PATH_INFO $fastcgi_path_info;
        fastcgi_pass "unix:///var/www/vhosts/system/example.com/php-fpm.sock";
        include /etc/nginx/fastcgi.conf;
    }

    location ~ /$ {
        index index.html index.cgi index.pl index.php index.xhtml index.htm index.shtml;
    }

    location /cat {
        try_files $uri $uri/ /index.php?url=$uri;
    }
}

0 个答案:

没有答案