大网数据广告活动
服务器运维问答

Nginx throws 404 only on php scripts using php-fpm

· 52CE 我爱测

I've installed a testing server using nginx + php-fpm . I've tried all of the following: Nginx + Php5-fpm not rendering php files nginx + php fpm -> 404 php pages - file not found When accessing PHP files, nginx throws an 404 err…

问题原文

原帖链接 · arielnmz · CC BY-SA 3.0

I've installed a testing server using nginx + php-fpm. I've tried all of the following:

Nginx + Php5-fpm not rendering php files

nginx + php fpm -> 404 php pages - file not found

When accessing PHP files, nginx throws an 404 error

Summarizing what I've tried:

  • Reinstalling.
  • Changing the script privileges (changed them to 0777).
  • fastcgi_intercept_errors on.
  • Checked the root directive at the levels: server, location and location ~ \.php.
  • Checked the fastcgi_param SCRIPT_FILENAME directive.

The server returns 404 on (and only on) .php scripts. I can rename them to .html and they'd be fine. How can I go about this?

This is my nginx.conf:

user nginx;
worker_processes 1;

error_log  /var/log/nginx/error.log;

pid        /run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

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

    sendfile        on;
    keepalive_timeout  2;

    include /etc/nginx/conf.d/*.conf;

    index   index.html index.htm;

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

        location / {
            root /var/www/html;
            index index.php index.html index.htm;
        }

        error_page  404              /404.html;
        location = /40x.html {
            #root /var/www/html;
        }


        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            #root /var/www/html;
        }

        location ~ \.php$ {
            root           /var/www/html;
            try_files $uri =404;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass   unix:/var/run/php5-fpm.sock;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }


    }

}

已采纳解答

原帖链接 · arielnmz · CC BY-SA 3.0

Solved it. It turns out that the problem was the permissions set on the socket where php was listening. I just had to change a directive called listen.mode on /etc/php-fpm.d/www.conf

listen.mode = 0750

And set the user to nginx:

listen.owner = nginx
listen.group = nginx

So the file looks like this:

srwxr-x---. 1 nginx nginx 0 jul  8 08:59 /var/run/php5-fpm.sock

Because I was using a unix socket instead of a tcp port:

listen = /var/run/php5-fpm.sock;

Also, I was getting 404 instead of 500 or 503 because my www.conf was configured to redirect errors to custom pages, and since they weren't there, I was getting 404's.

Edit:

It appears that in most recent versions of the nginx distribution in Fedora (Fedora 22, 23), nginx uses the apache user by default, and the socket is set to the user apache too, so no further configuration is needed.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2014-07-08)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。