1. nginx 1.21
1.1. 安裝


上傳到 linux 中,在 /usr/local 下

進入到 linux 中,到 /usr/local 下
# 切換到 /usr/local 下
[root@localhost ~]# cd /usr/local
# 解壓 nginx-1.21.0.tar.gz,別重命名為 nginx
[root@localhost local]# tar -zxvf nginx-1.21.0.tar.gz
# 安裝 nginx 環境依賴支持
[root@localhost local]# yum -y install pcre-devel openssl openssl-devel
# 進入到 nginx-1.21.0 里面
[root@localhost local]# cd nginx-1.21.0
[root@localhost nginx-1.21.0]# ./configure
# 執行安裝命令
[root@localhost nginx-1.21.0]# make install
執行完 make install 成功后,會在 nginx-1.21.0 的同級目錄下,生成 nginx 就是最終程序

配置環境變量
# 配置 nginx 環境變量
[root@localhost local]# vim /etc/profile

刷新環境配置
# 刷新配置
[root@localhost local]# source /etc/profile
開啟 nginx 服務
# 因為配置了環境變量,在哪里開啟,關閉,重啟都可以
# 開啟服務
[root@localhost local]# nginx
# 停止服務
[root@localhost local]# nginx -s stop
# 重啟服務
[root@localhost local]# nginx -s reload

配置反射代理
# 配置反射代理
[root@localhost local]# vim /usr/local/nginx/conf/nginx.conf
配置內容
# 配置信息 主機ip地址:端口號
proxy_pass http://192.168.190.128:8080;
# 配置完后,一定要重啟 nginx 服務
[root@localhost local]# nginx -s reload

最終效果

拒絕訪問
# 二選一
# 第一種解決方案,簡單高效,關閉防火牆
[root@localhost local]# systemctl stop firewalld.service
# 第二種解決方案,開放對應程序的端口號
# 查看程序 master 的 pid
[root@localhost local]# ps aux | grep nginx
root 22891 0.0 0.0 20712 1356 ? Ss 03:55 0:00 nginx: master process nginx
nobody 25369 0.0 0.0 23220 1744 ? S 04:01 0:00 nginx: worker process
root 43247 0.0 0.0 112808 964 pts/1 S+ 04:50 0:00 grep --color=auto nginx
# 通過上面,可以看到程序的 master 的 pid 是 22891
# 通過 pid 查看占用端口
[root@localhost local]# netstat -anp | grep 22891
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 22891/nginx: master
unix 3 [ ] STREAM CONNECTED 68105 22891/nginx: master
unix 3 [ ] STREAM CONNECTED 68104 22891/nginx: master
# 找到 tcp 里面包含的端口,通過上面信息可以看到是 80,那就打開80 端口訪問
[root@localhost local]# firewall-cmd --add-port=80/tcp --permanent
# 最后再重載生效剛才的端口設置
[root@localhost local]# firewall-cmd --reload
安裝完畢!
1.2. 卸載
# 查看是否安裝 nginx
[root@localhost local]# rpm -qa | grep nginx
[root@localhost local]# find / -name nginx
/usr/local/nginx
/usr/local/nginx/sbin/nginx
# 關閉 nginx 服務
[root@localhost local]# nginx -s stop
# 刪除或卸載
[root@localhost local]# rm -rf /usr/local/nginx
卸載完畢!