linux 下 nginx 安裝部署,反向代理tomcat等應用服務

一、準備

nginx 1.16.1

二、編譯安裝

安裝nginx編譯安裝的依賴軟件包

[root@pve-97 nginx]# yum install -y gcc gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel

 上傳nginx安裝包至相應目錄,這裏我們指定 /cjy/nginx

[root@pve-97 ~]# ll /ng/nginx/
total 1012
-rw-r--r--. 1 root root 1032630 Jan  8 14:35 nginx-1.16.1.tar.gz

解壓安裝包

[root@pve-97 nginx]# tar -zxvf nginx-1.16.1.tar.gz

解壓後目錄如下:

[root@pve-97 nginx-1.16.1]# ll
total 752
drwxr-xr-x. 6 1001 1001   4096 Jan  8 14:47 auto
-rw-r--r--. 1 1001 1001 296463 Aug 13 20:51 CHANGES
-rw-r--r--. 1 1001 1001 452171 Aug 13 20:51 CHANGES.ru
drwxr-xr-x. 2 1001 1001    168 Jan  8 14:47 conf
-rwxr-xr-x. 1 1001 1001   2502 Aug 13 20:51 configure
drwxr-xr-x. 4 1001 1001     72 Jan  8 14:47 contrib
drwxr-xr-x. 2 1001 1001     40 Jan  8 14:47 html
-rw-r--r--. 1 1001 1001   1397 Aug 13 20:51 LICENSE
drwxr-xr-x. 2 1001 1001     21 Jan  8 14:47 man
-rw-r--r--. 1 1001 1001     49 Aug 13 20:51 README
drwxr-xr-x. 9 1001 1001     91 Jan  8 14:47 src
  • auto:存放大量腳本,與根目錄configure文件相關;
  • conf:存放nginx的配置文件;
  • html:存放nginx首頁面和其他的html頁面;
  • man:存放nginx的的幫助文檔,安裝文成後可以使用man命令查看幫助;
  • src:存放nginx源代碼
  • configure文件:腳本文件,做一些準備工作,包括系統內核檢測、必須軟件庫檢測、參數解析、中間目錄生成,生成makefile文件等等

執行 ./configure 檢查並配置

[root@pve-97 nginx-1.16.1]# ./configure 
checking for OS
 + Linux 3.10.0-1062.el7.x86_64 x86_64
checking for C compiler ... found
 + using GNU C compiler
 + gcc version: 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC) 
checking for gcc -pipe switch ... found
checking for -Wl,-E switch ... found

...

Configuration summary
  + using system PCRE library
  + OpenSSL library is not used
  + using system zlib library

  nginx path prefix: "/usr/local/nginx"
  nginx binary file: "/usr/local/nginx/sbin/nginx"
  nginx modules path: "/usr/local/nginx/modules"
  nginx configuration prefix: "/usr/local/nginx/conf"
  nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
  nginx pid file: "/usr/local/nginx/logs/nginx.pid"
  nginx error log file: "/usr/local/nginx/logs/error.log"
  nginx http access log file: "/usr/local/nginx/logs/access.log"
  nginx http client request body temporary files: "client_body_temp"
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"

得到配置概要以後,就可以直接編譯和安裝了 make & make install

安裝完成後,nginx被默認安裝在 /usr/local/nginx 目錄

 

三、nginx開機啓動

創建文件nginx vi /lib/systemd/system/nginx.service

具體內容如下:

[Unit]  
Description=nginx  
After=network.target  
   
[Service]  
Type=forking  
ExecStart=/usr/local/nginx/sbin/nginx 
ExecReload=/usr/local/nginx/sbin/nginx -s reload  
ExecStop=/usr/local/nginx/sbin/nginx -s stop
PrivateTmp=true  
   
[Install]  
WantedBy=multi-user.target

設置開機啓動 systemctl enable nginx.service

[root@pve-97 init.d]# systemctl enable nginx.service
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.

使用服務命令啓動 systemctl start nginx

四、nginx Tomcat 負載均衡配置

打開路徑 /usr/local/nginx/conf/nginx.conf 編輯nginx配置文件

找到 http 模塊配置段

...略
http {
    ...略
    
    # 增加這個配置 (在這兒配置多個服務器)
    upstream servers_uias {
        # ip_hash指令,將同一用戶引入同一服務器。
        #ip_hash;
        server      127.0.0.1:801 weight=1; #weigth參數表示權值,權值越高被分配到的機率越大。詳細配置可見下章表格
        server      127.0.0.1:802 weight=1;
        server      127.0.0.1:803 weight=1;
    }
    
    #修改server
    server{
        # nginx監聽80端口
        listen      80;
      # 特別注意server_name配置,這兒在實際使用中配置多個域名,比如test.com,www.test.com。
        server_name uias.ng.com.cn 192.168.1.91;
        location / {
            root    html;
            index   index.html;
            proxy_pass      http://servers_uias; #這裏http://後面指向增加的upstream
            client_max_body_size    100m;
            proxy_set_header Host $host:$server_port;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Real-PORT $remote_port;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
    }
    ...略
}
...略

upstream的server標籤參數說明

  • server 127.0.0.1:801

負載均衡後面的RS配置,可以是IP或域名,如果端口不寫,默認是80端口。高併發場景下,IP可以換成域名,通過DNS做負載均衡。

  • weight

代表服務器的權重,默認值是1.權重數字越大表示接受的請求比例越大。 max_fails=1|nginx嘗試連接後端主機的次數,這個數值是配合proxy_next_upstream,fastcgi_next_upstream和memcached_next_upstream這三個參數來使用,當nginx接收後返回這三個參數定義的狀態碼時,會將這個請求轉發給正常工作的後端服務器,列如404,502,503,max_fails的默認值是1;企業場景:建議2-3次,京東1次,藍汛10次(CDN),根據業務需求去配置。

  • fail_timeout

在max_fails定義的失敗次數後,距離下次檢查的間隔時間,默認是10s,如果max_fails是5,它就檢測5次,如果5次都是502。那麼,他就會根據fail_timeout的值,等待10s再去檢查,還是隻檢查一次,如果持續502,在不重新加載nginx配置的情況下,每隔10s都只檢測一次,常規業務2-3秒比較合理,比如京東3秒,藍汛3秒,可根據業務需求去配置。

  • backup

熱備配置(RS節點的高可用),當前面激活的RS都失敗後會自動啓用熱備RS,這標誌這個服務器作爲備份服務器,若主服務器全部宕機了,就會向他轉發請求;注意,當負載調度算法爲ip_hash時,後端服務器在負載均衡調度中的狀態不能使weight和backup。

  • down

表示單前的server暫時不參與負載。

修改完成後 使用命令/usr/local/nginx/sbin/nginx -s reload 或 systemctl restart nginx

五、nginx的操作

啓動

1、直接執行二進制程序:

[root@iZwz9g2hqiikgs5lncf7f7Z nginx]# pwd
/usr/local/nginx
[root@iZwz9g2hqiikgs5lncf7f7Z nginx]#
[root@iZwz9g2hqiikgs5lncf7f7Z nginx]# ./sbin/nginx

這時,會讀取nginx安裝目錄下的配置文件:/usr/local/nginx/conf/nginx.conf

2、指定配置文件的方式啓動:

/usr/local/nginx/sbin/nginx –c /tmp/nginx.conf

這時,會讀取-c參數後指定的nginx.conf配置文件來啓動nginx。

3、指定安裝目錄的方式啓動

/usr/local/nginx/sbin/nginx –p /usr/local/nginx/

4、指定全局配置項的啓動方式

/usr/local/nginx/sbin/nginx –g “pid /var/nginx/test.pid”

這意味着nginx的pid文件會寫入到指定的目錄。-g參數不能與默認路徑下的nginx.conf配置衝突,否則無法成功啓動。

停止nginx

1、 快速停止 查找進程 ps -ef | grep nginx 殺掉進程 kill –s SIGTERM 10800 或 kill –s SIGINT 10800

2、使用stop命令 ./sbin/nginx –s stop

當快速停止服務時,worker進程與master進程在收到信號後會立刻跳出循環,退出進程。

3、平滑停止 停止master進程: ./sbin/nginx –s quit 等同於 kill -s SIGQUIT 停止work進程:kill -s SIGWINCH

平滑停止服務時,首先會關閉監聽端口,停止接收新的連接,然後把當前正在處理的連接全部處理完,最後再退出進程。

重新加載配置文件

/usr/local/nginx/sbin/nginx -s reload

Nginx會先檢查新的配置項是否有誤,如果全部正確就以“優雅”的方式關閉,再重新啓動Nginx來實現這個目的。類似的,-s是發送信號,仍然可以用kill命令發送HUP信號來達到相同的效果。 kill -s SIGHUP

日誌文件回滾

使用-s reopen參數可以重新打開日誌文件,這樣可以先把當前日誌文件改名或轉移到其他目錄中進行備份,再重新打開時就會生成新的日誌文件。這個功能使得日誌文件不至於過大。例如: /usr/local/nginx/sbin/nginx -s reopen 當然,這與使用kill命令發送USR1信號效果相同。kill -s SIGUSR1

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章