nginx配置不生效?

主机:阿里云ECS
环境:CentOS 7.6

反向代理和重定向都没有成功

PS:发现就连默认配置的欢迎页面也进不去...

default.conf配置如下:

server {
    listen                     80;
    server_name                127.0.0.1;
    error_page                 500 502 503 504 /50x.html;
    
    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }
    
    location /api {
        proxy_pass        http://127.0.0.1:5000;
        proxy_set_header  X-Real-IP $remote_addr;
    }

    location /baidu {
        return  302 https://www.baidu.com$request_uri;
    }
}

运行状态:

$ ps -ef |grep nginx

root     31126     1  0 02:42 ?        00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx    31127 31126  0 02:42 ?        00:00:00 nginx: worker process
root     31155 13304  0 02:46 ?        00:00:00 /opt/appnode/agent/apps/nginx/anapp-nginx
root     31212 31187  0 02:51 pts/0    00:00:00 grep --color=auto nginx

检查配置文件:

$ nginx -t

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

预期是:
访问 http://<主机IP> 会进入nginx的欢迎页面
访问 http://<主机IP>/api 会调用5000端口
访问 http://<主机IP>/baidu 则会重定向到baidu

请问这会是什么原因呢?
应该如何排查?
谢谢!!!

阅读 8.9k
4 个回答

服务器名设成这个

server_name                127.0.0.1;

你只能用 http://127.0.0.1/来访问,改改吧

把server_name注释掉试试呢?

你配置nginx.conf试试

你的 server_name 127.0.0.1; 监听的是本机,所以不能外网访问。只能通过 terminalcurl、wget 在本机上测试了

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题