centos8安裝Nginx1.19.6并配置多域名反向代理
- Nginx是什么?
- 為什么要使用Nginx?
- 安裝Nginx1.19.6
- 安裝依賴包
- 創建目錄
- 下載
- 解壓
- 編譯安裝
- 測驗Nginx組態檔是否正常
- 啟動Nginx
- 用戶和組
- 屬組和屬主
- Nginx的其他命令
- 加入systemctl管理并設定開機自啟動
- 將Nginx加入systemctl管理
- 創建nginx.service服務檔案
- 重新加載服務串列
- 啟動服務
- 將Nginx設定為開機自啟動
- Nginx配置反向代理
- 本機配置域名
- 配置反向代理
- 反向代理promethues
- 反向代理grafana
- 反向代理elasticsearch
- 反向代理logstash
- 反向代理kibana
- 反向代理tomcat
- 反向代理rocketmq控制臺
- 驗證
Nginx是什么?
Nginx是俄羅斯人Igor Sysoev撰寫的輕量級Web服務器,它的發音為 [?end??nks] ,它不僅是一個高性能的HTTP和反向代理服務器,同時也是一個IMAP/POP3/SMTP 代理服務器,
為什么要使用Nginx?
Nginx以事件驅動的方式撰寫,所以有非常好的性能,同時也是一個非常高效的反向代理、負載平衡服務器,在性能上,Nginx占用很少的系統資源,能支持更多的并發連接,達到更高的訪問效率;在功能上,Nginx是優秀的代理服務器和負載均衡服務器;在安裝配置上,Nginx安裝簡單、配置靈活,
Nginx支持熱部署,啟動速度特別快,還可以在不間斷服務的情況下對軟體版本或配置進行升級,即使運行數月也無需重新啟動,
在微服務的體系之下,Nginx正在被越來越多的專案采用作為網關來使用,配合Lua做限流、熔斷等控制,

對于大多數使用者來說,Nginx只是一個靜態檔案服務器或者http請求轉發器,它可以把靜態檔案的請求直接回傳靜態檔案資源,把動態檔案的請求轉發給后臺的處理程式,例如JAVA、php-fpm、apache、tomcat、jetty等,這些后臺服務,即使沒有nginx的情況下也是可以直接訪問的(有些時候這些服務器是放在防火墻的面,不是直接對外暴露,通過nginx做了轉換),
Nginx的優點:
- 高并發
- 占用資源少:2萬并發、10個執行緒,記憶體消耗幾百M,
- 功能種類比較多:web、cache、proxy,
- 支持epoll模型,使得nginx可以支持高并發,
- nginx 配合動態服務和Apache有區別,(FASTCGI 介面)
- 利用nginx可以對IP限速,可以限制連接數,
- 配置簡單,更靈活,
安裝Nginx1.19.6
安裝依賴包
[root@dxm28 /]# dnf -y install gcc pcre pcre-devel zlib zlib-devel openssl openssl-devel

- gcc:它可以編譯 C、C++、Ada、Object C和Java等語言,
- pcre pcre-devel:Pcre全稱(Perl Compatible Regular Expressions),是一個Perl庫,包括 perl兼容的正則運算式庫,nginx的http模塊使用pcre來決議正則運算式,
- zlib zlib-devel:zlib庫提供了很多種壓縮和解壓縮的方式,nginx使用zlib對http包的內容進行gzip,
- openssl openssl-devel:openssl是web安全通信的基石,沒有openssl,可以說我們的資訊都是在裸奔
創建目錄
[root@dxm28 /]# mkdir -p /data/nginx
[root@dxm28 /]# cd /data/nginx/
下載
[root@dxm28 nginx]# wget https://mirrors.huaweicloud.com/nginx/nginx-1.19.6.tar.gz
[root@dxm28 nginx]# ll -h

解壓
[root@dxm28 nginx]# tar -zxvf nginx-1.19.6.tar.gz
編譯安裝
使用./configure --help查看各個模塊的使用情況,
–without-http_ssi_module的方式關閉不需要的模塊
–with-http_perl_modules方式安裝需要的模塊
–prefix 指定安裝路徑
–with-http_stub_status_module允許查看nginx狀態的模塊
–with-http_ssl_module支持https的模塊
[root@dxm28 nginx]# cd nginx-1.19.6/
[root@dxm28 nginx-1.19.6]# ./configure --prefix=/usr/local/nginx --user=root --group=root --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module --with-http_sub_module --with-http_gzip_static_module --with-pcre

說明執行成功,接下來進行編譯和安裝:
[root@dxm28 nginx-1.19.6]# make && make install
測驗Nginx組態檔是否正常
[root@dxm28 nginx-1.19.6]# /usr/local/nginx/sbin/nginx -t

啟動Nginx
用戶和組
#先檢查用戶nginx和組nginx是否存在
#檢查組是否存在
cat /etc/group | grep nginx
#如果不存在,創建組
groupadd nginx
#檢查用戶nginx是否存在
cat /etc/passwd | grep nginx
或者
id nginx
#如果不存在,創建用戶
useradd -g nginx nginx
屬組和屬主
[root@dxm28 nginx-1.19.6]# chown -R nginx.nginx /usr/local/nginx

[root@dxm28 nginx-1.19.6]# su nginx
[nginx@dxm28 nginx-1.19.6]$ /usr/local/nginx/sbin/nginx

問題:nginx: [emerg] bind() to 0.0.0.0:80 failed (13: Permission denied)
原因:Linux只有root用戶可以使用1024以下的埠,
解決辦法:
- 以root權限啟動,
- 將 /usr/local/nginx/conf/nginx.conf檔案中的80埠改為1024以上,比如8082,
我們這里改成8082埠,再使用nginx用戶啟動,

查看一下行程和埠:
[nginx@dxm28 nginx-1.19.6]$ netstat -lntup |grep nginx
[nginx@dxm28 nginx-1.19.6]$ ps aux | grep nginx

驗證一下是否啟動成功:
[nginx@dxm28 nginx-1.19.6]$ curl http://localhost:8082

看到Welcome to nginx!,說明啟動成功了,我們瀏覽器訪問一下:
也可以使用root用戶啟動,后面我們還是改用root用戶啟動,埠還是80,
Nginx的其他命令
./nginx -s quit: (溫和)較stop相比就比較溫和一些了,此方式停止步驟是待nginx行程處理任務完畢進行停止,
./nginx -s stop: (強硬)此方式相當于先查出nginx行程id再使用kill命令強制殺掉行程,這種方法比較強硬,無論行程是否在作業,都直接停止行程,
./nginx -s reload 重啟nginx(不推薦此方法,推薦先停止在啟動)
./nginx -s reopen reopening the log files 用來打開日志檔案,這樣nginx會把新日志資訊寫入這個新的檔案中
加入systemctl管理并設定開機自啟動
將Nginx加入systemctl管理
創建nginx.service服務檔案
創建nginx.service服務檔案
[root@dxm28 nginx-1.19.6]# vim /etc/systemd/system/nginx.service
內容如下:
[Unit]
Description=nginx server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
User=root
Group=root
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
ExecQuit=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
重新加載服務串列
[root@dxm28 nginx-1.19.6]# systemctl daemon-reload
啟動服務
# 停止上面手動后臺啟動的nginx服務
[root@dxm28 nginx-1.19.6]# ps aux | grep nginx
root 73601 0.0 0.0 24420 820 ? Ss 18:27 0:00 nginx: master process /usr/local/nginx/sbin/nginx
root 73602 0.0 0.0 24788 2528 ? S 18:27 0:00 nginx: worker process
root 75427 0.0 0.0 12320 1076 pts/0 S+ 21:43 0:00 grep --color=auto nginx
[root@dxm28 nginx-1.19.6]$ /usr/local/nginx/sbin/nginx -s quit
# 檢查是否還存在nginx行程
[root@dxm28 nginx-1.19.6]# ps aux | grep nginx
root 75515 0.0 0.0 12320 1092 pts/0 S+ 21:48 0:00 grep --color=auto nginx
# 查詢所有服務單元是否有nginx
[root@dxm28 nginx-1.19.6]# systemctl list-unit-files | grep nginx
nginx.service disabled
# 存在,且非開啟自啟動,使用systemctl啟動nginx服務
[root@dxm28 nginx-1.19.6]# systemctl start nginx.service
# 查看nginx服務狀態
[root@dxm28 nginx-1.19.6]# systemctl status nginx.service

Active: active (running) 可以看到nginx服務已經啟動成功,查看行程以及埠的監聽情況:
[root@dxm28 logs]# ps aux | grep nginx
[root@dxm28 logs]# netstat -ntlp | grep 80

瀏覽器訪問http://192.168.0.28/

將Nginx設定為開機自啟動
[root@dxm28 nginx-1.19.6]# systemctl list-unit-files | grep nginx
nginx.service disabled
# disabled表示非開機自啟動
# 設定為開機自啟動
[root@dxm28 nginx-1.19.6]# systemctl enable nginx.service
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /etc/systemd/system/nginx.service.
# 再次查看
[root@dxm28 nginx-1.19.6]# systemctl list-unit-files | grep nginx
nginx.service enabled
# enabled表示是開機自啟動,執行重啟命令
[root@dxm28 nginx-1.19.6]# reboot
#重啟后再次查看nginx服務已啟動完成,
Nginx配置反向代理
基于前幾篇文章,我們已經搭建好了prometheus+node_exporter+grafana、redis集群、elk、rocketmq集群、tomcat等服務,那么我們接下來就抽取其中的幾個服務作為例子來配置一下反向代理吧,
本機配置域名
修改/etc/hosts組態檔加入promethues、grafana、elasticsearch、logstash、kibana、tomcat、rocketmq控制臺的host映射,如下:
#配置nginx反向代理開始
192.168.0.28 promethues.dxm.com
192.168.0.28 grafana.dxm.com
192.168.0.28 elasticsearch.dxm.com
192.168.0.28 logstash.dxm.com
192.168.0.28 kibana.dxm.com
192.168.0.28 tomcat.dxm.com
192.168.0.28 rocketmqconsole.dxm.com
#配置nginx反向代理結束
配置反向代理
首先在主組態檔(nginx.conf)中添加一行代碼:
# 將這些代碼寫在http模塊中
include prometheus.conf;
include grafana.conf;
include elasticsearch.conf;
include logstash.conf;
include kibana.conf;
include tomcat.conf;
include rocketmqconsole.conf;
注意位置千萬不要寫錯,否則配置不生效,
組態檔的名字一定要寫*.conf 如:promethues.conf grafana.conf
反向代理promethues
在/usr/local/nginx/conf/目錄下新增加一個prometheus.conf組態檔,server配置如下:
server {
listen 80;
server_name promethues.dxm.com;
location / {
proxy_pass http://192.168.0.22:9090;
index index.html index.htm index.jsp;
}
}
反向代理grafana
在/usr/local/nginx/conf/目錄下新增加一個grafana.conf組態檔,server配置如下:
server {
listen 80;
server_name grafana.dxm.com;
location / {
proxy_pass http://192.168.0.22:3000;
index index.html index.htm index.jsp;
}
}
反向代理elasticsearch
在/usr/local/nginx/conf/目錄下新增加一個elasticsearch.conf組態檔,server配置如下:
server {
listen 80;
server_name elasticsearch.dxm.com;
location / {
proxy_pass http://192.168.0.27:9200;
index index.html index.htm index.jsp;
}
}
反向代理logstash
在/usr/local/nginx/conf/目錄下新增加一個logstash.conf組態檔,server配置如下:
server {
listen 80;
server_name logstash.dxm.com;
location / {
proxy_pass http://192.168.0.27:9200;
index index.html index.htm index.jsp;
}
}
反向代理kibana
在/usr/local/nginx/conf/目錄下新增加一個kibana.conf組態檔,server配置如下:
server {
listen 80;
server_name kibana.dxm.com;
location / {
proxy_pass http://192.168.0.27:5601;
index index.html index.htm index.jsp;
}
}
反向代理tomcat
在/usr/local/nginx/conf/目錄下新增加一個tomcat.conf組態檔,server配置如下:
server {
listen 80;
server_name tomcat.dxm.com;
location / {
proxy_pass http://127.0.0.1:8080;
index index.html index.htm index.jsp;
}
}
反向代理rocketmq控制臺
在/usr/local/nginx/conf/目錄下新增加一個rocketmqconsole.conf組態檔,server配置如下:
server {
listen 80;
server_name rocketmqconsole.dxm.com;
location / {
proxy_pass http://192.168.0.27:8080;
index index.html index.htm index.jsp;
}
}
驗證
上面兩步配置完成之后,就可以在本機直接通過上面hosts里面系結的域名進行訪問,如圖:
prometheus.dxm.com:

logstash.dxm.com:

tomcat.dxm.com:

grafana.dxm.com:
rocketmqconsole.dxm.com:

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/240476.html
標籤:其他
上一篇:vscode在Linux環境下運行“權限不夠”的解決方法
下一篇:部署HPC集群的實施方案
