【问题标题】:Nginx works strange with try_files directiveNginx 与 try_files 指令一起工作很奇怪
【发布时间】:2020-12-05 06:15:38
【问题描述】:

我在尝试进行站点配置时遇到了奇怪的 Nginx 行为。我需要 Nginx 使用 article.php 以防服务器上缺少文件。但它的工作原理很奇怪。如果查询是example.com/snaff,它将被重定向到article.php。但是如果查询是 example.com/snaff.php 它只会在浏览器中抛出 404 错误。

我使用 Ubuntu 20.04 + Nginx 1.18.0。早些时候,相同的配置在 Ubuntu 18.04 + Nginx 1.19.0 上运行良好。

不明白出了什么问题。

这是配置代码。

server {
    listen 80;
    server_name example.com www.example.com;
    #return 404; # managed by Certbot

    if ($host = www.example.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot

    if ($host = example.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot
}

server {
    listen 443 ssl http2;

    server_name example.com www.example.com;
    root /var/www/example.com/public_html/;

    index index.php index.html index.htm;

    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/example.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

    access_log /var/log/nginx/example_access.log;
    error_log /var/log/nginx/example_error.log error;

    etag on;

    gzip            on; 
    gzip_comp_level 5;
    gzip_min_length 10;
    gzip_proxied    any;
    gzip_types      *;
    gzip_disable "msie6";

    client_max_body_size 15m;

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
        add_header Cache-Control "no-store, no-cache, must-revalidate";
    }

    location ~ /\.ht {
        deny all;
    }

    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        try_files $uri $uri/ /article.php;
    }
}

【问题讨论】:

    标签: php nginx


    【解决方案1】:

    也许您的 snippets/fastcgi-php.conf 文件随着新发行版的更改而更改为这个:

    # regex to split $uri to $fastcgi_script_name and $fastcgi_path
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    
    # Check that the PHP script exists before passing it
    try_files $fastcgi_script_name =404;
    
    # Bypass the fact that try_files resets $fastcgi_path_info
    # see: http://trac.nginx.org/nginx/ticket/321
    set $path_info $fastcgi_path_info;
    fastcgi_param PATH_INFO $path_info;
    
    fastcgi_index index.php;
    include fastcgi.conf;
    

    问题的原因是try_files $fastcgi_script_name =404; 行。我建议你完全删除这个 sn-p 并使用更简单的东西,比如

    location ~ \.php$ {
        try_files $fastcgi_script_name /article.php;
        include fastcgi.conf;
        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
        add_header Cache-Control "no-store, no-cache, must-revalidate";
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-01-26
      • 2019-05-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-23
      • 2012-04-24
      相关资源
      最近更新 更多