一、前置操作
為了排除其它因素干擾,可以先暫時關閉防火牆和SELinux,待成功后再逐一開啟
# 關閉防火牆
systemctl stop firewalld.service
# 關閉SELinux
setenforce 0
二、Nginx官方yum源配置
新建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
運行命令安裝Nginx
yum install -y nginx.x86_64
啟動Nginx
systemctl start nginx.service
訪問Nginx,若返回"Welcome to nginx!"字眼則安裝成功
curl 'http://127.0.0.1'
四、配置Nginx
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.conf
a.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