在配置后台项目环境虚拟主机时,出现了 网址拒绝了我们的连接请求,net::ERR_CONNECTION_REFUSED
这样的错误。
但是配置的localhost:8080是可以正常访问的,hosts文件也已经修改。
于是虚心(作死)的开始了调试,但是最痛苦的是无论是nginx 还是php-fpm都没有明确的错误提示。这就很郁闷了。
百度一波,结果没有匹配的解决方案。
但是自己只修改了/usr/local/etc/nginx/nginx.conf的配置,所以只能跟它刚一波了。
最后改了无数次配置,然后和同事请教了之后,终于跑通了。
先放一波Mac brew 安装的nginx默认配置
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include 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 logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 8080;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# 正常来说配置虚拟主机只需要在这里进行配置就好了,打开server前面的注释
# another virtual host using mix of IP-, name-, and port-based configuration
#
server {
listen 8081; #虚拟主机的端口 不能和默认的8080一样
#listen noob.local:8081; #监听接口也可以这样写
server_name noob.local; #访问域名
location / {
root /users/kano/noob; #访问目录
index index.html index.htm index.php;#添加index.php支持 不能放第一位
}添加.php文件转发到fastcgi处理()
location ~ \.php$ {
root html;#默认
fastcgi_pass 127.0.0.1:9000;#默认
fastcgi_index index.php;#默认
fastcgi_param SCRIPT_FILENAME /users/kano/noob$fastcgi_script_name;#添加访问目录
include fastcgi_params; #默认 } } # HTTPS server # #server { # listen 443 ssl; # server_name localhost; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } #} include servers/*;}这样配置后noob.local:8081就可以访问了。
另外一种配置虚拟主机的方法是通过servers目录添加多台虚拟主机。
include servers/*;在/usr/local/etc/nginx/servers里添加虚拟主机的配置文件 noob.conf
里面只需要配置server
server {
listen 8081;
server_name noob.local;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root /users/kano/noob;
index index.html index.php;
}
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /users/kano/noob$fastcgi_script_name;
include fastcgi_params;
}
}这样的话再添加虚拟主机,只需要修改访问域名和文件目录就可以了。
配置nginx虚拟主机时遇到net::ERR_CONNECTION_REFUSED错误,本地localhost:8080可正常访问。排查过程中发现nginx和php-fpm无明确错误提示。通过逐步调试,最终修改/usr/local/etc/nginx/nginx.conf,实现.php文件转发到fastcgi处理,并在servers目录添加虚拟主机配置文件解决问题。
3506





