标签 nginx 下的文章

背景

公司部门内,配置了内网使用的域名。服务器部署完成后,nginx配置时,使用了该域名。请求后发现抛出了502 Bad Gateway

原因

查看nginx日志,发现了如下信息

2022/03/14 09:04:19 [error] 27#27: *41 no resolver defined to resolve ***.***.com, client: 10.10.60.155, server: ***.***.com, request: "POST /demo/captcha/anon/getLoginCaptcha HTTP/1.1", host: "***.***.com", referrer: "http://***.***.com/login/login"

很明显,是因为nginx无法解析该域名导致了。我们只需要让nginx能识别该域名即可。

解决方案

解决方法很简单,只需要在nginx全局配置(nginx.conf)的http项内,添加对应的dns解析即可resolver dns服务器地址;。如下所示:

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;
    resolver 10.10.10.1; #配置dns地址
    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  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}

背景

在云服务器上搭建了wordpress,为了方便部署,采用了docker方式,使用了9102的端口号。然后通过nginx进行反向代理,使用域名访问。

问题

nginx配置很简单,直接使用了proxy_pass配置到docker容器的wordpress地址,如下:

server {
    listen 80;
    server_name www.我的域名.com;
    location / {
        proxy_pass http://10.0.4.6:9102;
        proxy_set_header Host $host;
        proxy_set_header X-Forward-For $remote_addr;
        proxy_redirect off;
    }
}

配置完成后,访问域名进行安装,配置,都正常。管理后台也可正常访问。但是想要进入博客首页时,出了问题,直接跳转到了http://10.0.4.6:9102。很明显,这个地址是无法访问也是不对的。
总结下问题:域名+/其他页面的形式是可以正常访问的,但是如果只有域名来访问首页就会出现301重定向问题。

Request URL: http://我的域名.com/
Request Method: GET
Status Code: 301 Moved Permanently (from disk cache)
Remote Address: 127.0.0.1:8889
Referrer Policy: strict-origin-when-cross-origin

Content-Length: 0
Content-Type: text/html; charset=UTF-8
Date: Thu, 19 Aug 2021 06:42:17 GMT
Location: http://10.0.4.5/
Server: nginx/1.21.1
X-Powered-By: PHP/7.4.22
X-Redirect-By: WordPress

解决方案:

多方查找,找到了方案,安装如下配置进行设置,即可解决。

server {
    listen       80;
    server_name  我的域名.com;
    
    set $node_port 9102;
    location / {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_set_header X-NginX-Proxy true;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_pass http://10.0.4.5:$node_port$request_uri;
        proxy_redirect off;
    }
}

问题介绍

nginx执行nginx -s reload时,出现如下错误:

nginx: [error] CreateFile() "C:\web\nginx-1.17.9/logs/nginx.pid" failed (2: The system cannot find the file specified)

排查方法

执行命令nginx -t查看配置文件是否正确,大部分问题都是因为配置文件造成的,此命令会列出配置文件错误项。如下:

C:\web\nginx-1.17.9>nginx.exe -t
nginx: the configuration file C:\web\nginx-1.17.9/conf/nginx.conf syntax is ok
nginx: [emerg] bind() to 0.0.0.0:8090 failed (10013: An attempt was made to access a socket in a way forbidden by its access permissions)
nginx: configuration file C:\web\nginx-1.17.9/conf/nginx.conf test failed

可以看出,配置文件中监听端口号无法正常启动,应是被占用了,修改端口之后,重新执行nginx -s reload即可。

P.S. 本人nginx并不熟悉,此文也仅是记录下本次碰到此问题的解决过程。故此方案未必能解决所有同类问题。