一、步骤还原
1.通过yum命令安装nginx
sudo yum install nginx
2.在 /etc/nginx/conf.d/ 目录下创建一个新的配置文件 proxy.conf,内容如下
server {
listen 8080;
server_name 公网IP;
location / {
proxy_pass http://127.0.0.1:7860;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
3.检查配置文件无误
[root@ ~]# sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
4.重启nginx服务开放端口
sudo systemctl restart nginx
firewall-cmd --zone=public --add-port=8080/tcp --permanent
5.重启防火墙检查端口是否开放
firewall-cmd --reload
firewall-cmd --zone=public --list-ports
7.浏览器访问发现提示502
注意:当我开放7860端口时,直接使用公网IP:7860访问时正常的
二、解决方案
出现这个问题主要是因为SELinux,这里科普一下,SELinux(Security-Enhanced Linux)是Linux内核中的一个安全模块,它提供了访问控制和安全策略强制执行支持。当启用 SELinux 后,系统会强制应用安全策略以确保所有进程和服务均受到适当的安全保护。这可以防止很多类型的攻击,例如缓冲区溢出和代码注入攻击。
1.查看SELinux状态,“SELinux status” 行中显示 “enabled”,表示 SELinux 已启用。
[root@~]# sestatus
SELinux status: enabled
SELinuxfs mount: /sys/fs/selinux
SELinux root directory: /etc/selinux
Loaded policy name: targeted
Current mode: enforcing
Mode from config file: enforcing
Policy MLS status: enabled
Policy deny_unknown status: allowed
Max kernel policy version: 31
2.要解决这个问题,有如下几种方案
- 临时禁用 SELinux
使用以下命令来暂时禁用 SELinux,如果要启用则 sudo setenforce 1
sudo setenforce 0
- 配置 SELinux 策略
通过配置 SELinux 策略来允许访问需要的端口和服务,这将确保 SELinux 的 http_port_t 类型允许访问 3000 端口
sudo semanage port -a -t http_port_t -p tcp 7860
- 彻底禁用 SELinux
如果你确定不需要 SELinux,并且它会导致更多的问题,请彻底禁用 SELinux。你可以通过编辑 /etc/selinux/config 文件并将 SELINUX 参数设置为 disabled,然后重启系统以使更改生效
SELINUX=disabled
注意,禁用 SELinux 可能会降低系统安全性
当我们选择上述的一种方案时,我们在浏览器就能够正常访问了