php项目中出现 upstream: “fastcgi://unix:/var/run/php-cgi.sock“,

本文介绍了一种常见的Nginx问题——404 Not Found错误,并提供了具体的解决方案。当遇到此问题时,检查错误日志可以发现与fastcgi连接相关的问题,通过重启php-fpm服务通常能够有效解决该问题。

项目中出现nginx 404 not found.

查看error.log后发现 "fastcgi://unix:/var/run/php-cgi.sock",

出现这种情况时,重启一下php-fpm即可解决问题。

## # You should look at the following URL's in order to grasp a solid understanding # of Nginx configuration files in order to fully unleash the power of Nginx. # https://www.nginx.com/resources/wiki/start/ # https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/ # https://wiki.debian.org/Nginx/DirectoryStructure # # In most cases, administrators will remove this file from sites-enabled/ and # leave it as reference inside of sites-available where it will continue to be # updated by the nginx packaging team. # # This file will automatically load configuration files provided by other # applications, such as Drupal or Wordpress. These applications will be made # available underneath a path with that package name, such as /drupal8. # # Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples. ## # Default server configuration # #server { # listen 80; # listen [::]:80; # server_name jenkins.eftong.cn; # return 301 https://$server_name$request_uri; #} #server { # SSL configuration # # listen [::]:443 ssl ipv6only=on; # managed by Certbot # listen 443 ssl; # managed by Certbot # ssl_certificate /etc/ssl/certs/4177192_jenkins.eftong.cn.pem; # ssl_certificate_key /etc/ssl/certs/4177192_jenkins.eftong.cn.key; # access_log /var/log/nginx/jenkins.access.log; # error_log /var/log/nginx/jenkins.error.log; # listen 443 ssl default_server; # listen [::]:443 ssl default_server; # # Note: You should disable gzip for SSL traffic. # See: https://bugs.debian.org/773332 # # Read up on ssl_ciphers to ensure a secure configuration. # See: https://bugs.debian.org/765782 # # Self signed certs generated by the ssl-cert package # Don't use them in a production server! # # include snippets/snakeoil.conf; # root /var/www/html; # Add index.php to the list if you are using PHP # index index.html index.htm index.nginx-debian.html; # server_name jenkins.eftong.cn; # location / { # First attempt to serve request as file, then # as directory, then fall back to displaying a 404. #try_files $uri $uri/ =404; # include /etc/nginx/proxy_params; # proxy_pass http://localhost:8888; # proxy_read_timeout 90s; # Fix potential "It appears that your reverse proxy setup is broken" error. # proxy_redirect http://localhost:8888 https://jenkins.eftong.cn; # } # pass PHP scripts to FastCGI server # #location ~ \.php$ { # include snippets/fastcgi-php.conf; # # # With php-fpm (or other unix sockets): # fastcgi_pass unix:/var/run/php/php7.0-fpm.sock; # # With php-cgi (or other tcp sockets): # fastcgi_pass 127.0.0.1:9000; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} #} server { listen 8304; server_name 11.21.1.120; location ^~ /WEB-INF { deny all; } location / { root /home/hcare/support/application/EFT-P9/frontend/client/html; index index.html index.htm; try_files $uri $uri/ /index.html; } location ^~ /auth/ { proxy_set_header Host $host:$server_port; proxy_set_header X-Forwaded-For $remote_addr; proxy_set_header X-Forwaded-Scheme $scheme; proxy_pass http://127.0.0.1:8302; } location ^~ /operator/ { proxy_set_header Host $host:$server_port; proxy_set_header X-Forwaded-For $remote_addr; proxy_set_header X-Forwaded-Scheme $scheme; proxy_pass http://127.0.0.1:8302; } location ^~ /service/ { proxy_set_header Host $host:$server_port; proxy_set_header X-Forwaded-For $remote_addr; proxy_set_header X-Forwaded-Scheme $scheme; proxy_pass http://127.0.0.1:8302; } location ^~ /his/ { proxy_set_header Host $host:$server_port; proxy_set_header X-Forwaded-For $remote_addr; proxy_set_header X-Forwaded-Scheme $scheme; proxy_pass http://127.0.0.1:8302; } location ^~ /static/ { root /home/hcare/support/application/EFT-P9/frontend/client/html; } location ~ .*\.(gif|jpg|jpeg|png)$ { expires 24h; root /data/EFT-P9/pic/;#指定图片存放路径 #access_log /usr/local/nginx/logs/images.log;#日志存放路径 proxy_store on; proxy_store_access user:rw group:rw all:rw; proxy_temp_path /data/EFT-P9/pic/;#图片访问路径 proxy_redirect off; proxy_set_header Host 127.0.0.1; client_max_body_size 10m; client_body_buffer_size 1280k; proxy_connect_timeout 900; proxy_send_timeout 900; proxy_read_timeout 900; proxy_buffer_size 40k; proxy_buffers 40 320k; proxy_busy_buffers_size 640k; proxy_temp_file_write_size 640k; if ( !-e $request_filename) { proxy_pass http://127.0.0.1;#默认80端口 } } location ~ .*\.(txt|pdf|doc|docx|xls|xlsx)$ { expires 24h; root /data/EFT-P9/file/;#指定图片存放路径 #access_log /usr/local/nginx/logs/images.log;#日志存放路径 proxy_store on; proxy_store_access user:rw group:rw all:rw; proxy_temp_path /data/EFT-P9/file/;#访问路径 proxy_redirect off; proxy_set_header Host 127.0.0.1; client_max_body_size 10m; client_body_buffer_size 1280k; proxy_connect_timeout 900; proxy_send_timeout 900; proxy_read_timeout 900; proxy_buffer_size 40k; proxy_buffers 40 320k; proxy_busy_buffers_size 640k; proxy_temp_file_write_size 640k; if ( !-e $request_filename) { proxy_pass http://127.0.0.1;#默认80端口 } } error_page 404 = http://11.21.1.120:8304/#/404; } # Virtual Host configuration for example.com # # You can move that to a different file under sites-available/ and symlink that # to sites-enabled/ to enable it. # #server { # listen 80; # listen [::]:80; # # server_name example.com; # # root /var/www/example.com; # index index.html; # # location / { # try_files $uri $uri/ =404; # } #}
09-05
server { listen 80; listen 443 ssl http2 ; server_name jiujie.com www.jiujie.com; index index.php index.html index.htm default.php default.htm default.html; root /www/wwwroot/jiujie.com; #CERT-APPLY-CHECK--START # 用于SSL证书申请时的文件验证相关配置 -- 请勿删除 include /www/server/panel/vhost/nginx/well-known/jiujie.com.conf; #CERT-APPLY-CHECK--END #SSL-START SSL相关配置,请勿删除或修改下一行带注释的404规则 #error_page 404/404.html; #HTTP_TO_HTTPS_START if ($server_port !~ 443){ rewrite ^(/.*)$ https://$host$1 permanent; } #HTTP_TO_HTTPS_END ssl_certificate /www/server/panel/vhost/cert/jiujie.com/fullchain.pem; ssl_certificate_key /www/server/panel/vhost/cert/jiujie.com/privkey.pem; ssl_protocols TLSv1.1 TLSv1.2 TLSv1.3; ssl_ciphers EECDH+CHACHA20:EECDH+CHACHA20-draft:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5; ssl_prefer_server_ciphers on; ssl_session_cache shared:SSL:10m; ssl_session_timeout 10m; add_header Strict-Transport-Security "max-age=31536000"; error_page 497 https://$host$request_uri; #SSL-END #ERROR-PAGE-START 错误页配置,可以注释、删除或修改 #error_page 404 /404.html; #error_page 502 /502.html; #ERROR-PAGE-END #PHP-INFO-START PHP引用配置,可以注释或修改 include enable-php-54.conf; #PHP-INFO-END #REWRITE-START URL重写规则引用,修改后将导致面板设置的伪静态规则失效 include /www/server/panel/vhost/rewrite/jiujie.com.conf; #REWRITE-END #禁止访问的文件或目录 location ~ ^/(\.user.ini|\.htaccess|\.git|\.env|\.svn|\.project|LICENSE|README.md) { return 404; } #一键申请SSL证书验证目录相关设置 location ~ \.well-known{ allow all; } #禁止在证书验证目录放入敏感文件 if ( $uri ~ "^/\.well-known/.*\.(php|jsp|py|js|css|lua|ts|go|zip|tar\.gz|rar|7z|sql|bak)$" ) { return 403; } location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ { expires 30d; error_log /dev/null; access_log /dev/null; } location ~ .*\.(js|css)?$ { expires 12h; error_log /dev/null; access_log /dev/null; } access_log /www/wwwlogs/jiujie.com.log; error_log /www/wwwlogs/jiujie.com.error.log; }
最新发布
12-03
<think>我们正在解决Nginx连接PHP-FPM套接字失败的问题,具体错误是“php-fpm.sock:Nosuchfileordirectory”。根据之前提供的解决指南和引用内容,我们将分步骤排查。关键点:这个错误通常意味着Nginx配置中指定的PHP-FPM套接字文件路径不正确,或者PHP-FPM没有成功创建该套接字文件。步骤概览:1.确认PHP-FPM配置中监听的套接字路径(www.conf)2.确认Nginx配置中fastcgi_pass指向的套接字路径(server配置中的location~\.php$部分)3.检查套接字文件是否存在,并确认权限4.重启PHP-FPM服务以重新生成套接字(如果缺失)5.检查SELinux是否阻止了套接字的创建或访问6.检查错误日志详细步骤:步骤1:检查PHP-FPM配置打开PHP-FPM配置文件(通常为/etc/php-fpm.d/www.conf),查找以下行:```listen=/var/run/php-fpm/php-fpm.sock```注意:实际路径可能不同,常见路径还有`/run/php-fpm/www.sock`等。请记下这个路径。步骤2:检查Nginx配置在Nginx的站点配置文件中(例如/etc/nginx/conf.d/default.conf),检查处理PHP的location块:```location~\.php${fastcgi_passunix:/var/run/php-fpm/php-fpm.sock;...#其他参数}```确保`fastcgi_pass`指令后面的套接字路径与PHP-FPM配置中的路径完全一致。如果不一致,修改Nginx配置中的路径(或者PHP-FPM的配置)使二者一致。步骤3:检查套接字文件是否存在使用命令检查套接字文件:```bashls-l/var/run/php-fpm/php-fpm.sock```注意:如果路径是`/run/php-fpm/php-fpm.sock`,那么`/run`是`/var/run`的符号链接,两者是同一个位置。如果文件不存在,尝试重启PHP-FPM服务:```bashsystemctlrestartphp-fpm```然后再次检查。步骤4:检查套接字文件权限如果文件存在,检查其权限和所属用户/组。PHP-FPM配置中通常有设置:```listen.owner=nginxlisten.group=nginxlisten.mode=0660```同时,确保运行Nginx的用户(通常是nginx)有权限访问这个套接字(即用户nginx在listen.group的组中)。查看套接字文件的权限:```bashls-l/var/run/php-fpm/php-fpm.sock```预期输出类似:```srw-rw----.1rootnginx0Aug1615:14/var/run/php-fpm/php-fpm.sock```注意:这里的所有者可能是root或php-fpm的运行用户(如www-data),但组必须是nginx(或者Nginx运行用户所在的组,确保Nginx有权限访问)。如果权限不对,修改PHP-FPM配置中的listen.owner、listen.group和listen.mode,然后重启PHP-FPM。步骤5:检查SELinux如果上述都正确,可能是SELinux阻止了访问。检查SELinux状态:```bashsestatus```如果是enforcing状态,尝试临时禁用SELinux以排除问题:```bashsetenforce0```然后重新访问,如果问题解决,说明是SELinux的问题。需要设置正确的SELinux规则:方法1:将SELinux设置为允许模式(permissive),但不推荐方法2:允许httpd(Nginx)连接外部的网络套接字(因为Unix域套接字被视为网络):```bashsetsebool-Phttpd_can_network_connect1```或者允许访问套接字文件(需要根据实际路径调整):```bashchcon-Rtsock_file_t/var/run/php-fpm/php-fpm.sock```或者尝试恢复默认上下文:```bashrestorecon-v/var/run/php-fpm/php-fpm.sock```步骤6:查看日志如果问题仍未解决,检查Nginx错误日志和PHP-FPM日志:-Nginx错误日志:`/var/log/nginx/error.log`-PHP-FPM日志:通常在`/var/log/php-fpm.log`或`/var/log/php-fpm/www-error.log`(取决于配置)在Nginx错误日志中查找类似:```connect()tounix:/var/run/php-fpm/php-fpm.sockfailed(2:Nosuchfileordirectory)```如果有权限问题,日志会有:```connect()tounix:/var/run/php-fpm/php-fpm.sockfailed(13:Permissiondenied)```解决方案总结:1.确保路径一致(PHP-FPM和Nginx配置)2.确保PHP-FPM服务正常运行,且套接字文件已生成(重启服务)3.确保套接字文件的权限和所属用户组正确(Nginx运行用户有访问权限)4.如果是SELinux问题,调整规则或临时禁用(生产环境谨慎)根据引用[1]中提到的命令,重启PHP-FPM和Nginx服务:```bashsystemctlrestartphp-fpmnginx-sreload```或者引用中使用的命令:`/etc/init.d/php-fpmrestart`(适用于旧版系统,新版推荐systemctl)引用[2]中提到了安装PHPPHP-FPM,如果之前没有安装,可以使用:```bashsudoyuminstallphpphp-fpm```引用[3]提到乱码问题,在配置时要注意避免特殊字符和乱码,确保配置文件格式正确。最后,生成相关问题:§§相关问题§§1.如何修改PHP-FPM的监听方式为TCP端口?2.SELinux如何放行特定服务的端口或套接字?3.Nginx如何配置多个站点的PHP解析?</think> Nginx 连接 PHP-FPM 套接字失败 (`No such file or directory`) 的解决方案 这个问题通常由 PHP-FPM 服务未启动或路径配置不一致导致,以下是系统化的解决步骤: --- ### 🔧 步骤1:确认 PHP-FPM 服务状态 ```bash systemctl status php-fpm ``` - ✅ **若未运行**: ```bash systemctl start php-fpm # 启动服务 systemctl enable php-fpm # 设置开机自启 ``` - ❌ **启动失败**: ```bash journalctl -xe -u php-fpm # 查看详细错误日志 ``` --- ### 📍 步骤2:验证套接字路径一致性 1. **查看 PHP-FPM 监听路径**: ```bash grep ^listen /etc/php-fpm.d/www.conf ``` 输出示例: `listen = /var/run/php-fpm/php-fpm.sock` [^2] 2. **检查 Nginx 配置**: ```bash grep fastcgi_pass /etc/nginx/conf.d/*.conf ``` - 🔍 确保路径与 PHP-FPM 的 `listen` 值**完全相同** - 📝 修改 Nginx 配置示例: ```nginx location ~ \.php$ { fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock; # 与PHP-FPM路径一致 } ``` --- ### 🔐 步骤3:修复套接字文件权限 ```bash # 检查套接字文件是否存在 ls -l /var/run/php-fpm/ # 若文件缺失/权限错误: rm -f /var/run/php-fpm/php-fpm.sock # 删除残余文件 systemctl restart php-fpm # 重建套接字 chown nginx:nginx /var/run/php-fpm/php-fpm.sock chmod 660 /var/run/php-fpm/php-fpm.sock ``` --- ### 🛡️ 步骤4:处理 SELinux 限制 ```bash # 临时放行(测试用) setenforce 0 # 永久解决方案 semanage fcontext -a -t httpd_var_run_t "/var/run/php-fpm(/.*)?" restorecon -Rv /var/run/php-fpm setsebool -P httpd_can_network_connect on ``` --- ### 📋 步骤5:验证服务联动 1. 创建测试文件: ```bash echo "<?php phpinfo(); ?>" > /var/www/html/test.php ``` 2. 重启服务: ```bash systemctl restart php-fpm nginx ``` 3. 访问 `http://your-IP/test.php` ✅ 显示 PHP 信息页表示成功 --- ### ⚠️ 常见错误解决 | 错误现象 | 解决方案 | |----------|----------| | `connect() failed (111: Connection refused)` | `systemctl restart php-fpm` | | `Permission denied (13)` | `chown nginx:nginx /var/run/php-fpm/*` + SELinux 配置 | | 套接字文件不自动生成 | 检查 `/var/run` 是否具有 `tmpfiles.d` 配置支持 | --- ### 🔍 深度排查工具 ```bash # 实时监控日志 tail -f /var/log/nginx/error.log /var/log/php-fpm.log # 检查套接字通信 ss -xlp | grep php-fpm ``` ![](https://example.com/socket-diagram.png) *图:Nginx 与 PHP-FPM 通过 Unix Socket 通信流程 [^1]* ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

芝麻开门2015

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值