nginx环境配置--**拒绝了我们的连接请求

配置nginx虚拟主机时遇到net::ERR_CONNECTION_REFUSED错误,本地localhost:8080可正常访问。排查过程中发现nginx和php-fpm无明确错误提示。通过逐步调试,最终修改/usr/local/etc/nginx/nginx.conf,实现.php文件转发到fastcgi处理,并在servers目录添加虚拟主机配置文件解决问题。

在配置后台项目环境虚拟主机时,出现了  网址拒绝了我们的连接请求,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.exe` 时,如果遇到连接请求拒绝的问题,可能的原因和解决方案如下: 1. **Nginx 服务未启动** 如果 Nginx 没有正确启动,客户端无法连接。可以通过命令 `nginx` 启动服务。如果已经启动,尝试使用 `nginx -s reload` 重新加载配置[^1]。 2. **端口被占用或配置错误** 默认情况下,Nginx 监听 80 端口。如果该端口被其他程序占用,或者配置文件中指定了错误的端口,会导致连接失败。检查 `nginx.conf` 文件中的 `listen` 指令,确保其配置正确,例如 `listen 80;`[^3]。 3. **防火墙或安全策略限制** 本地防火墙或操作系统安全策略可能阻止了对 Nginx 的访问。检查系统防火墙设置,确保允许通过 Nginx 所使用的端口(如 80 或 443)。 4. **配置文件语法错误** 如果配置文件中存在语法错误,Nginx 可能无法正常启动。使用命令 `nginx -t` 可以测试配置文件的语法是否正确,并修复发现的问题[^1]。 5. **文件路径配置错误** 如果使用了 `root` 或 `alias` 指令来指定文件路径,但配置不当,可能导致资源无法正确加载。例如,`root` 和 `alias` 在解析 `location` 后的 URI 时方式不同,需确保其使用方式符合预期。例如: ```nginx location / { root html; index index.html index.htm; } ``` 确保 `root` 指向的目录结构正确,并且 `index.html` 存在[^3]。 6. **权限问题** Nginx 需要足够的权限来访问其配置文件、日志文件以及静态资源目录。如果权限配置不当,可能导致服务启动失败或资源无法访问。检查日志文件(如 `logs/error.log`)以获取更多信息,并调整文件权限设置[^3]。 7. **Windows 环境下的路径问题** 在 Windows 上运行 Nginx 时,路径中的反斜杠 `\` 需要正确转义为 `/` 或双反斜杠 `\\`,否则可能导致配置解析失败。例如: ```nginx root C:/nginx/html; ``` 或者 ```nginx root C:\\nginx\\html; ``` 8. **反向代理配置问题** 如果使用 Nginx 作为反向代理,确保 `proxy_pass` 指令正确指向后端服务,并且目标服务器正在运行。例如: ```nginx location /api/ { proxy_pass http://backend_server; } ``` 检查后端服务器是否正常响应,并确保网络可达性[^4]。 9. **检查日志信息** Nginx 的错误日志通常位于 `logs/error.log`,可以通过查看日志文件来定位连接拒绝的具体原因。启用详细的日志记录有助于排查问题,例如: ```nginx error_log logs/error.log info; ``` 10. **重启 Nginx 服务** 在修改了配置文件或解决了潜在问题后,尝试重启 Nginx 服务以确保更改生效。可以使用以下命令: ```bash nginx -s stop nginx ```
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值