【问题标题】:Try_files for php causing error on fastcgi-php.conf, "try_files" directive is duplicate in /etc/nginx/snippets/fastcgi-php.conf:5Try_files for php 在 fastcgi-php.conf 上导致错误,“try_files”指令在 /etc/nginx/snippets/fastcgi-php.conf:5 中重复
【发布时间】:2017-08-26 21:40:45
【问题描述】:

我尝试添加 try_files 来检查 php 文件是否存在,但它给了我错误。

"try_files" directive is duplicate in /etc/nginx/snippets/fastcgi-php.conf:5

这里是我要添加try_files的位置

location ~ \.php$ {
    try_files $uri =404;
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/run/php/php7.0-fpm-user.sock;
}

这是我的竞争服务器块。

server {
    listen 80;
    server_name www.example.com example.com;
    return 301 https://example.com$request_uri;
}



server {
    listen 443 ssl;
    server_name www.example.com;
    ssl_certificate /home/user/ssl/example.com.ssl-bundle.crt;
    ssl_certificate_key /home/user/ssl/example.com.key;
    return 301 https://example.com$request_uri;
}



server {
    listen 443 ssl;

    root /home/user/public_html;
    index index.php index.html index.htm index.nginx-debian.html;

    server_name example.com;
    ssl_certificate /home/user/ssl/example.com.ssl-bundle.crt;
    ssl_certificate_key /home/user/ssl/example.com.key;

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

    location = /robots.txt {
        allow all;
        log_not_found off;
        access_log off;
    }

    include /home/user/public_html/nginx.conf;

    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    location ^~ /wp-login.php {
        auth_basic "Administrator Login";
        auth_basic_user_file /etc/nginx/.htpasswd;
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.0-fpm-user.sock;
    }

    location /phpmyadmin {
        auth_basic "Restricted Content";
        auth_basic_user_file /etc/nginx/.htpasswd;
    }

    # Phpmyadmin Configurations
    location /phpmyadmin {
        root /home/user/public_html/;
        index index.php index.html index.htm;
        auth_basic "Restricted Content";
        auth_basic_user_file /etc/nginx/.htpasswd;
        location ~ ^/phpmyadmin/(.+\.php)$ {
            try_files $uri =404;
            root /home/user/public_html/;
            fastcgi_pass unix:/run/php/php7.0-fpm-user.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
        }
        location ~* ^/phpmyadmin/(.+\.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$ {
            root /home/user/public_html/;
        }
    }

    location /phpMyAdmin {
        rewrite ^/* /phpmyadmin last;
    }

    location ~ \.php$ {
        try_files $uri =404;
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.0-fpm-user.sock;
    }

    location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
        expires max;
        log_not_found off;
    }

}

我的服务器块有什么问题?为什么 try_files 给我错误?它是 fastcgi-php.conf 错误吗?我的 Nginx 版本是 1.12.1

另一个问题,请检查我的 phpmyadmin 块,我从这里得到它https://serverfault.com/questions/433785/nginx-phpmyadmin-redirecting-to-instead-of-phpmyadmin-upon-login 这种格式有效,但据我所知,root 不应该有斜杠。

谢谢

【问题讨论】:

    标签: php nginx


    【解决方案1】:

    从这个块中删除try_files

    location ~ \.php$ {
        try_files $uri =404;
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.0-fpm-user.sock;
    }
    

    您已经在 snippets/fastcgi-php.conf; 中有它导致重复错误

    【讨论】:

    • 我明白了,但为什么我也不能使用try_files $uri =403;?如果我使用snippets/fastcgi-php.conf;,我什至不能使用不同的try_files 编辑:我的错,不能在一个位置使用超过1 个try_files。我现在很累 :D 关于 phpmyadmin root 使用斜杠的第二个问题怎么样?
    • @somuch72,尽量在帖子中保留一个问题。所以更容易评论和发布解决方案
    【解决方案2】:

    我有同样的问题, 正如错误所说,有重复的通用配置,nginx 不喜欢它。你需要的一切都只是去

    /etc/nginx/snippets/fastcgi-php.conf:[line5]
    

    并在该行的开头添加一个“#”,您可能需要在此文件中更改更多关于您的 nginx 配置的行

    【讨论】:

    • 虽然这会停止错误状态 - 它也会产生停止解析 PHP 的不良影响。
    猜你喜欢
    • 1970-01-01
    • 2018-10-23
    • 2013-12-23
    • 2018-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-13
    • 1970-01-01
    相关资源
    最近更新 更多