Nginx使用中的小问题 作者: Hogwarts 发布于: 2022-12-26 更新于: 2023-12-21 分类: 默认分类 #问题 **问题1**、使用 NGINX 官方源安装,启动程序发现 Systemd 中存在报错:Can't open PID file /var/run/nginx.pid (yet?) after start: No such file or directory **原因**:Nginx 启动时 PID 文件并未生成,导致文件无法读取 **解决**:在 /usr/lib/systemd/system/nginx.service 中添加如下语句即可,作用是在执行可执行文件前等待 0.1s。 ExecStartPost=/bin/sleep 0.1 **参考**:`https://www.24kplus.com/others/1770.html` **问题2**、使用反代时报错nginx: [emerg] unknown "connection_upgrade" variable **解决**:nginx配置文件出了问题,将下面map代码块补到 http {}中即可。 map $http_upgrade $connection_upgrade { default upgrade; '' close; } **参考**: `https://github.com/jupyterhub/jupyterhub/issues/781` `https://segmentfault.com/a/1190000018712908` **问题3**、connect() to unix:/run/php/php8.2-fpm.sock failed (13: Permission denied) while connecting to upstream **原因**:由于/run/php/php8.2-fpm.sock的用户是www-data而 nginx 的用户是 nginx,用户名字不匹配造成权限问题。 **解决**:把 nginx 的权限改为 www-data。将nginx.conf中的 user:nginx 改为user:www-data 或者给予权限 chown -R nginx:nginx /run/php/php8.2-fpm.sock **问题4**、/var/cache/nginx/proxy_temp/9/17/0000000179" failed (13: Permission denied) while reading upstream **原因**:从用户nginx改为www-data后,www-data没有访问的权限。 **解决**:停止nginx进程,`rm -rf /var/cache/nginx/*` **参考**:`https://serverfault.com/questions/235154/permission-denied-while-reading-upstream` **问题5**、default.conf nginx: [warn] protocol options redefined for 0.0.0.0:443 in /etc/nginx/sites-enabled/default:3 nginx: [warn] protocol options redefined for [::]:443 in /etc/nginx/sites-enabled/default:4 **解决**:您可以轻松地手动修复它,只需在此文件/etc/nginx/sites-enabled/default 的“listen”(仅限 443)指令中添加“http2”参数即可。 default.conf的路径可能需修改。 listen 443 ssl http2 default_server; listen [::]:443 ssl http2 default_server; **参考**:`https://webinoly.com/support/8851/nginx-warn-protocol-options-redefined` **问题6**:open_basedir PHP message: PHP Warning: realpath(): open_basedir restriction in effect. **解决**:这个问题很复杂。我认为是PHP默认路径和网站存储目录的问题,还有nginx默认目录的问题。盲猜要是网站目录在/var/www/下应该无此问题。 可改php.ini或nginx解决,本例以nginx为例 在conf的php配置中添加 fastcgi_param PHP_VALUE "open_basedir=/网站目录/:/tmp/:/proc/"; **参考**:[open_basedir 限制生效。文件 (/) 不在允许的路径内](https://stackoverflow.com/questions/1846882/open-basedir-restriction-in-effect-file-is-not-within-the-allowed-paths "open_basedir 限制生效。文件 (/) 不在允许的路径内") [“require(): open_basedir restriction in effect. ” 解决方法](https://www.jianshu.com/p/b9bc7c4fd401 "“require(): open_basedir restriction in effect. ” 解决方法") **问题7**:Index of 汉字乱码 **解决**:在server的配置内容中,增加一行:`charset utf-8;` charset utf-8; #Index of 界面显示中文,不乱码。 autoindex on; #开启nginx目录浏览功能 autoindex_format html; #以html风格将目录展示在浏览器中 autoindex_exact_size on; #文件大小从KB开始显示 autoindex_localtime on; #显示文件修改时间为服务器本地时间 **参考**:[Nginx 显示中文乱码解决](https://blog.csdn.net/qq_35448976/article/details/79256873 "Nginx 显示中文乱码解决") **问题8**:执行 nginx -t 后,在执行 nginx -s reload 的时候报错 `nginx: [error] invalid PID number "" in "/run/nginx.pid"` 解决: pkill -9 nginx nginx -c /etc/nginx/nginx.conf nginx -s reload **问题9**:connect() to unix:/run/php/php8.2-fpm.sock failed (13: Permission denied) while connecting to upstream 参考:https://winamp.top/107.html 解决: vim /etc/php/8.2/fpm/pool.d/www.conf nginx的运行用户为nginx,php8.2的运行用户为www-data 将www-data改为nginx listen.owner = nginx listen.group = nginx **问题10**:Nginx 调整文件上传大小限制 nginx文件上传默认是1MB client_max_body_size 用来修改允许客户端上传文件的大小。默认为1m,**如果设置为0,表示上传文件大小不受限制。**可以在以下模块设置: http, server, location client_max_body_size 100m; 标签: nginx