为了排除其它因素干扰,可以先暂时关闭防火墙和SELinux,待成功后再逐一开启
# 关闭防火墙 systemctl stop firewalld.service # 关闭SELinux setenforce 0新建repo文件
vim /etc/yum.repos.d/nginx.repo根据实际版本、架构配置yum源
[nginx] name=nginx repo baseurl=http://nginx.org/packages/centos/$verson/$basearch/ gpgcheck=0 enabled=1运行命令安装Nginx
yum install -y nginx.x86_64启动Nginx
systemctl start nginx.service访问Nginx,若返回"Welcome to nginx!"字眼则安装成功
curl 'http://127.0.0.1'根据域名转发端口,如访问a.com转发到内网的127.0.0.1:8080
备份默认配置文件,新建配置文件a.conf
mv /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/default.conf.bak touch /etc/nginx/conf.d/a.confa.conf文件配置如下
server { # 监听80端口 listen 80; # 监听a.com域名 server_name a.com; # 记录入口日志 access_log /var/log/nginx/a.access.log main; # 记录错误日志 error_log /var/log/nginx/a.error.log; location / { # 反向代理 proxy_pass http://127.0.0.1:8080; } #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 /usr/share/nginx/html; } }此时访问a.com会转发到内网127.0.0.1:8080上
其它可能会用到的命令
# CentOS7 永久开放80端口 firewall-cmd --zone=public --add-port=80/tcp --permanent firewall-cmd --reload # SELinux 允许通过网络连接到httpd服务(解决'... connect() to 127.0.0.1:8080 failed (13: Permission denied) while connecting to upstream ...'问题) setsebool -P httpd_can_network_connect 1