centos8安裝Nginx1.19.6並配置多域名反向代理
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
也可以使用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
將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: