使用nginx时遇到的各种问题

在使用nginx的过程中遇到了一些问题,在这里总结到一起,会陆续更新。

一、启动nginx时出现 nginx: [emerg] bind() to 0.0.0.0:80 failed (13: Permission denied)

cd /usr/local/nginx/sbin
./nginx

这个问题是因为当前用户对当前的位置没有写入权限。

sudo /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

以root权限来启动nginx。

二、访问时出现 403 Forbidden

配置文件当中的

location / {  
                index  index.html index.htm;  
                #index.php  
                autoindex  on;  
           }

在直接访问这个地址时,nginx会搜索index,index.html,index.htm这三个文件。如果文件都不存在,就会报403。

三、访问时出现 connect() failed (111: Connection refused) while connecting to upstream

服务器报了502错误,在nginx的日志文件中出现了这个报错:connect() failed (111: Connection refused) while connecting to upstream。

第一次出现这个问题的时候,在网上搜了很多资料,大部分都是复制粘贴,并没有完美解决我的问题。最后在外网转了一圈,找到了答案。

首先,检查nginx的配置文件fastcgi_pass

location ~ \.php {
      		fastcgi_pass   127.0.0.1:9000;
      		#fastcgi_pass unix:/tmp/php-cgi.sock;
      		fastcgi_index index.php;
	  		fastcgi_split_path_info ^(.+\.php)(.*)$;
	  		fastcgi_param PATH_INFO $fastcgi_path_info;
      		include fastcgi.conf;
    	}

fastcgi监听9000端口.

随后去更改fpm的配置文件,我的配置文件在/etc/php/7.1/fpm/pool.d/www.conf

取消掉listen.allowed_clients = 127.0.0.1的注释,并将原来listen的内容(我的是listen = /run/php/php7.1-fpm.sock)改为listen = 9000。

sudo service php7.1-npm restart
sudo service nginx restart

随后重启php-npm,重启nginx。

发表评论