Linux上安裝nginx+tomcat負載均衡


1、Ngnix

  Nginx (發音同 engine x)是一款輕量級的Web 服務器/反向代理服務器及電子郵件(IMAP/POP3)代理服務器,並在一個BSD-like 協議下發行。  其特點是占有內存少,並發能力強,事實上nginx的並發能力確實在同類型的網頁伺服器中表現較好。目前中國大陸使用nginx網站用戶有:新浪、網易、 騰訊,另外知名的微網志Plurk也使用nginx。

工作原理圖如下:

2、Tomcat

tomcat服務器我們可以准備2、3個tomcat服務器進行測試。

安裝就不多說了(不會的看這),直接看配置:

依次修改3台tomcat的配置文件vi /software/tomcat-8.5.30/conf/server.xml

... 
 <Server port="18005" shutdown="SHUTDOWN">
...
...
<Connector port="18080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" /> 
...
...
<Connector port="18009" protocol="AJP/1.3" redirectPort="8443" /> 
 ...
... 
 <Server port="28005" shutdown="SHUTDOWN">
...
...
<Connector port="28080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" /> 
...
...
<Connector port="28009" protocol="AJP/1.3" redirectPort="8443" /> 
 ...
... 
 <Server port="38005" shutdown="SHUTDOWN">
...
...
<Connector port="38080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" /> 
...
...
<Connector port="38009" protocol="AJP/1.3" redirectPort="8443" /> 
 ...

沒有配置全局變量就這樣子啟動和關閉:

sh /software/tomcat-8.5.30/bin/startup.sh
sh /software/tomcat-8.5.30/bin/shutdown.sh

OK,需要配置的就配置好了,我們再做一件事,區別tomcat的歡迎頁,我們修改一下index.jsp頁面,tomcat-8.5.30/webapps/ROOT/index.jsp,隨意區分一下就好。然后分別啟動三個tomcat。

3、環境要求

1、 jdk 1.8.0_102(不會安裝的看這里

2、nginx 1.12.0(在官網上下一個解壓就行,官網:http://nginx.org/);

3、2個或多個tomcat 6.x 7.x 8.x 9.x 都可以(比如我准備的是3個一模一樣的8.x版本,只是配置文件改了而已,一會會詳細說怎么改 ,官網:http://tomcat.apache.org/)

4、Ngnix配置

a、安裝依賴:

yum install gcc
yum install gcc-c++ yum install prce pcre
-devel yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel

b、安裝nginx

也可進入此鏈接下載下面所需所有包https://download.csdn.net/download/MobiusStrip/12262519

進入:/software/nginx位置

  下載nginx: wget http://nginx.org/download/nginx-1.12.2.tar.gz
  下載openssl : wget http://www.openssl.org/source/openssl-fips-2.0.9.tar.gz
  下載zlib : wget http://zlib.net/zlib-1.2.11.tar.gz
  下載pcre : wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.38.tar.gz

(如果上面的包找不到,那就在自己電腦上下載然后上傳即可,官網:http://nginx.org/en/download.html)

安裝perl:   (手動下載網址https://www.cpan.org/src/README.html

[root@localhost ~] wget https://www.cpan.org/src/5.0/perl-5.30.2.tar.gz
[root@localhost ~] tar -xzf perl-5.30.2.tar.gz -C /software
[root@localhost ~] cd perl-5.30.2
[root@localhost ~] ./Configure -des -Dprefix=$HOME/localperl
[root@localhost ~] make
[root@localhost ~] make test
[root@localhost ~] make install

安裝openssl:

[root@localhost ~] tar zxvf openssl-fips-2.0.9.tar.gz -C /software
[root@localhost ~] cd /software/openssl-fips-2.0.9
[root@localhost ~] ./config && make && make install

安裝pcre:

[root@localhost ~] tar zxvf pcre-8.38.tar.gz -C /software
[root@localhost ~] cd /software/pcre-8.38
[root@localhost ~] ./configure && make && make install

安裝zlib:

[root@localhost ~] tar zxvf zlib-1.2.11.tar.gz -C /software
[root@localhost ~] cd /software/zlib-1.2.11
[root@localhost ~] ./configure && make && make install

安裝ngnix:

[root@localhost ~] tar zxvf nginx-1.12.2.tar.gz -C /software
[root@localhost ~] cd /software/nginx-1.12.2
[root@localhost ~] ./configure && make && make install

c、配置nginx

配置路徑在 /usr/local/nginx/conf  ; 因為安裝路徑就是在這里。

簡易直接配置:

 1 #Nginx所用用戶和組,window下不指定
 2 user  root root;
 3 #工作的子進程數量(通常等於CPU數量或者2倍於CPU)
 4 worker_processes  2;
 5 #錯誤日志存放路徑
 6 #error_log  logs/error.log;
 7 #error_log  logs/error.log  notice;
 8 error_log  logs/error.log  info;
 9 #指定pid存放文件
10 #pid        logs/nginx.pid;
11  
12 events {
13 #使用網絡IO模型linux建議epoll,FreeBSD建議采用kqueue,window下不指定。
14 #use epoll;
15 #允許最大連接數
16 worker_connections  2048;
17 }
18  
19 http {
20 include       mime.types;
21 default_type  application/octet-stream;
22 #定義日志格式
23 log_format  main  '$remote_addr - $remote_user [$time_local] $request '
24                  '"$status" $body_bytes_sent "$http_referer" '
25                  '"$http_user_agent" "$http_x_forwarded_for"';
26 #access_log  off;
27  
28 access_log  logs/access.log;
29  
30 client_header_timeout  3m;
31 client_body_timeout    3m;
32 send_timeout           3m;
33 #client_header_buffer_size    16k;
34 #large_client_header_buffers  4 16k;
35 sendfile        on;
36 tcp_nopush      on;
37 tcp_nodelay     on;
38 #keepalive_timeout  75 20;
39 #include    gzip.conf;
40  
41 #負載均衡配置
42 #mmzsblog.cn和下文的server_name要一致
43 upstream mmzsblog.cn {
44     #根據ip計算將請求分配各那個后端tomcat,許多人誤認為可以解決session問題,其實並不能。
45     #同一機器在多網情況下,路由切換,ip可能不同
46     #ip_hash;
47     #server 118.24.19.22:18080;
48     #server 118.24.19.22:28080;
49     #server 118.24.19.22:38080;
50 
51     #upstream的負載均衡,weight是權重,可以根據機器配置定義權重。weigth參數表示權值,權值越高被分配到的幾率越大。
52     server 118.24.19.22:18081 weight=2;
53     server 118.24.19.22:28080 weight=3;
54     server 118.24.19.22:38080 weight=2;
55 }
56 server {
57 listen       80;
58 server_name  mmzsblog.cn;
59 location / {
60 proxy_connect_timeout   3;
61 proxy_send_timeout      30;
62 proxy_read_timeout      30;
63 
64 add_header X-Static transfer;
65 proxy_redirect off;
66 proxy_set_header Host $host;
67 proxy_set_header X-Real-IP $remote_addr;
68 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
69 
70 #如果是本機就用localhost
71 proxy_pass http://mmzsblog.cn;
72 }
73  
74 #css|js|ico|gif|jpg|jpeg|png|txt|html|htm|xml|swf|wav這些都是靜態文件,但應分辨,js、css可能經常會變,過期時間應小一些,圖片、html基本不變,過期時間可以設長一些
75         location ~* ^.+\.(ico|gif|jpg|jpeg|png|html|htm)$ {
76             root         html;
77             access_log   logs/access.log;
78             expires      -1s;
79         }
80  
81 gzip on;
82         gzip_comp_level 7;
83         gzip_min_length  1100; #需要壓縮的最小長度
84         gzip_buffers    4 8k;
85         gzip_types      text/plain application/javascript text/css text/xml application/x-httpd-php; #指定需要壓縮的文件類型
86         output_buffers  1 32k;
87         postpone_output  1460;
88  
89         #error_page  404              /404.html;
90  
91         # redirect server error pages to the static page /50x.html
92         error_page   500 502 503 504  /50x.html;
93         location = /50x.html {
94             root   html;
95         }
96 }
97 }
/software/nginx-1.12.2/conf/nginx.conf

帶中文詳細注解配置,可根據需要自行修改:

  1 #Nginx所用用戶和組,window下不指定
  2 user  root root;
  3 #工作的子進程數量(通常等於CPU數量或者2倍於CPU)
  4 worker_processes  2;
  5 #錯誤日志存放路徑
  6 #error_log  logs/error.log;
  7 #error_log  logs/error.log  notice;
  8 error_log  logs/error.log  info;
  9 #指定pid存放文件
 10 #pid        logs/nginx.pid;
 11 
 12 #指定進程可以打開的最大描述符:數目
 13 #工作模式與連接數上限
 14 #這個指令是指當一個nginx進程打開的最多文件描述符數目,理論值應該是最多打開文件數(ulimit -n)與nginx進程數相除,但是nginx分配請求並不是那么均勻,所以最好與ulimit -n 的值保持一致。
 15 #現在在linux 2.6內核下開啟文件打開數為65535,worker_rlimit_nofile就相應應該填寫65535。
 16 #這是因為nginx調度時分配請求到進程並不是那么的均衡,所以假如填寫10240,總並發量達到3-4萬時就有進程可能超過10240了,這時會返回502錯誤。
 17 worker_rlimit_nofile 65535;
 18  
 19 events {
 20     #參考事件模型,use [ kqueue | rtsig | epoll | /dev/poll | select | poll ]; epoll模型
 21     #是Linux 2.6以上版本內核中的高性能網絡I/O模型,linux建議epoll,如果跑在FreeBSD上面,就用kqueue模型。
 22     #補充說明:
 23     #與apache相類,nginx針對不同的操作系統,有不同的事件模型
 24     #A)標准事件模型
 25     #Select、poll屬於標准事件模型,如果當前系統不存在更有效的方法,nginx會選擇select或poll
 26     #B)高效事件模型
 27     #Kqueue:使用於FreeBSD 4.1+, OpenBSD 2.9+, NetBSD 2.0 和 MacOS X.使用雙處理器的MacOS X系統使用kqueue可能會造成內核崩潰。
 28     #Epoll:使用於Linux內核2.6版本及以后的系統。
 29     #/dev/poll:使用於Solaris 7 11/99+,HP/UX 11.22+ (eventport),IRIX 6.5.15+ 和 Tru64 UNIX 5.1A+ 30     #Eventport:使用於Solaris 10。 為了防止出現內核崩潰的問題, 有必要安裝安全補丁。
 31     #使用網絡IO模型linux建議epoll,FreeBSD建議采用kqueue,window下不指定。
 32     #use epoll;
 33 
 34     #單個進程最大連接數(最大連接數=連接數*進程數)
 35     #根據硬件調整,和前面工作進程配合起來用,盡量大,但是別把cpu跑到100%就行。每個進程允許的最多連接數,理論上每台nginx服務器的最大連接數為。    
 36     worker_connections  2048;
 37     
 38     #keepalive超時時間。
 39     keepalive_timeout 60;
 40 
 41     #客戶端請求頭部的緩沖區大小。這個可以根據你的系統分頁大小來設置,一般一個請求頭的大小不會超過1k,不過由於一般系統分頁都要大於1k,所以這里設置為分頁大小。
 42     #分頁大小可以用命令getconf PAGESIZE 取得。
 43     #[root@web001 ~]# getconf PAGESIZE
 44     #4096
 45     #但也有client_header_buffer_size超過4k的情況,但是client_header_buffer_size該值必須設置為“系統分頁大小”的整倍數。
 46     client_header_buffer_size 4k;
 47 
 48     #這個將為打開文件指定緩存,默認是沒有啟用的,max指定緩存數量,建議和打開文件數一致,inactive是指經過多長時間文件沒被請求后刪除緩存。
 49     open_file_cache max=65535 inactive=60s;
 50 
 51     #這個是指多長時間檢查一次緩存的有效信息。
 52     #語法:open_file_cache_valid time 默認值:open_file_cache_valid 60 使用字段:http, server, location 這個指令指定了何時需要檢查open_file_cache中緩存項目的有效信息.
 53     open_file_cache_valid 80s;
 54 
 55     #open_file_cache指令中的inactive參數時間內文件的最少使用次數,如果超過這個數字,文件描述符一直是在緩存中打開的,如上例,如果有一個文件在inactive時間內一次沒被使用,它將被移除。
 56     #語法:open_file_cache_min_uses number 默認值:open_file_cache_min_uses 1 使用字段:http, server, location  這個指令指定了在open_file_cache指令無效的參數中一定的時間范圍內可以使用的最小文件數,如果使用更大的值,文件描述符在cache中總是打開狀態.
 57     open_file_cache_min_uses 1;
 58     
 59     #語法:open_file_cache_errors on | off 默認值:open_file_cache_errors off 使用字段:http, server, location 這個指令指定是否在搜索一個文件是記錄cache錯誤.
 60     open_file_cache_errors on;
 61 }
 62  
 63 http {
 64     #文件擴展名與文件類型映射表
 65     include       mime.types;
 66 
 67     #默認文件類型
 68     default_type  application/octet-stream;
 69 
 70     #服務器名字的hash表大小
 71     #保存服務器名字的hash表是由指令server_names_hash_max_size 和server_names_hash_bucket_size所控制的。參數hash bucket size總是等於hash表的大小,並且是一路處理器緩存大小的倍數。在減少了在內存中的存取次數后,使在處理器中加速查找hash表鍵值成為可能。如果hash bucket size等於一路處理器緩存的大小,那么在查找鍵的時候,最壞的情況下在內存中查找的次數為2。第一次是確定存儲單元的地址,第二次是在存儲單元中查找鍵 值。因此,如果Nginx給出需要增大hash max size 或 hash bucket size的提示,那么首要的是增大前一個參數的大小.
 72     server_names_hash_bucket_size 128;
 73     
 74     #客戶端請求頭部的緩沖區大小。這個可以根據你的系統分頁大小來設置,一般一個請求的頭部大小不會超過1k,不過由於一般系統分頁都要大於1k,所以這里設置為分頁大小。分頁大小可以用命令getconf PAGESIZE取得。
 75     client_header_buffer_size 32k;
 76 
 77     #客戶請求頭緩沖大小。nginx默認會用client_header_buffer_size這個buffer來讀取header值,如果header過大,它會使用large_client_header_buffers來讀取。
 78     large_client_header_buffers 4 64k;
 79     
 80     #設定通過nginx上傳文件的大小
 81     client_max_body_size 8m;
 82     
 83     #定義日志格式
 84     log_format  main  '$remote_addr - $remote_user [$time_local] $request '
 85                      '"$status" $body_bytes_sent "$http_referer" '
 86                      '"$http_user_agent" "$http_x_forwarded_for"';
 87     #access_log  off;
 88     #定義本虛擬主機的訪問日志
 89     access_log  /usr/local/nginx/logs/host.access.log  main;
 90     access_log  /usr/local/nginx/logs/host.access.404.log  log404; 
 91 
 92  
 93     #請求超時設置
 94     client_header_timeout  3m;
 95     client_body_timeout    3m;
 96     send_timeout           3m;
 97 
 98     #開啟高效文件傳輸模式,sendfile指令指定nginx是否調用sendfile函數來輸出文件,對於普通應用設為 on,如果用來進行下載等應用磁盤IO重負載應用,可設置為off,以平衡磁盤與網絡I/O處理速度,降低系統的負載。注意:如果圖片顯示不正常把這個改成off。
 99     #sendfile指令指定 nginx 是否調用sendfile 函數(zero copy 方式)來輸出文件,對於普通應用,必須設為on。如果用來進行下載等應用磁盤IO重負載應用,可設置為off,以平衡磁盤與網絡IO處理速度,降低系統uptime。
100     sendfile on;
101 
102     #開啟目錄列表訪問,合適下載服務器,默認關閉。
103     autoindex on;
104 
105     #此選項允許或禁止使用socke的TCP_CORK的選項,此選項僅在使用sendfile的時候使用
106     tcp_nopush on;
107      
108     tcp_nodelay on;
109 
110     #長連接超時時間,單位是秒
111     keepalive_timeout 120;
112 
113     #FastCGI相關參數是為了改善網站的性能:減少資源占用,提高訪問速度。下面參數看字面意思都能理解。
114     fastcgi_connect_timeout 300;
115     fastcgi_send_timeout 300;
116     fastcgi_read_timeout 300;
117     fastcgi_buffer_size 64k;
118     fastcgi_buffers 4 64k;
119     fastcgi_busy_buffers_size 128k;
120     fastcgi_temp_file_write_size 128k;
121 
122     #gzip模塊設置
123     gzip on; #開啟gzip壓縮輸出
124     gzip_min_length 1k;    #最小壓縮文件大小
125     gzip_buffers 4 16k;    #壓縮緩沖區
126     gzip_http_version 1.0;    #壓縮版本(默認1.1,前端如果是squid2.5請使用1.0127     gzip_comp_level 2;    #壓縮等級
128     gzip_types text/plain application/x-javascript text/css application/xml;    #壓縮類型,默認就已經包含textml,所以下面就不用再寫了,寫上去也不會有問題,但是會有一個warn。
129     gzip_vary on;
130 
131     #開啟限制IP連接數的時候需要使用
132     #limit_zone crawler $binary_remote_addr 10m;
133 
134 
135     #注:proxy_temp_path和proxy_cache_path指定的路徑必須在同一分區
136     proxy_temp_path   /usr/local/www/proxy_temp_dir;
137     #設置Web緩存區名稱為cache_one,內存緩存空間大小為200MB,1天沒有被訪問的內容自動清除,硬盤緩存空間大小為30GB。
138     proxy_cache_path  /usr/local/www/proxy_cache_dir  levels=1:2   keys_zone=cache_one:200m inactive=1d max_size=30g;
139 
140     #include    gzip.conf;
141  
142  
143     #負載均衡配置
144     #mmzsblog.cn和下文的server_name要一致
145     upstream mmzsblog.cn {
146     #根據ip計算將請求分配各那個后端tomcat,許多人誤認為可以解決session問題,其實並不能。
147     #同一機器在多網情況下,路由切換,ip可能不同
148     #ip_hash;
149     #server 118.24.19.22:18080;
150     #server 118.24.19.22:28080;
151     #server 118.24.19.22:38080;
152 
153     #upstream的負載均衡,weight是權重,可以根據機器配置定義權重。weigth參數表示權值,權值越高被分配到的幾率越大。
154     server 118.24.19.22:18081 weight=2;
155     server 118.24.19.22:28080 weight=3;
156     server 118.24.19.22:38080 weight=2;
157 
158 #nginx的upstream目前支持4種方式的分配
159         #1、輪詢(默認)
160         #每個請求按時間順序逐一分配到不同的后端服務器,如果后端服務器down掉,能自動剔除。
161         #2、weight
162         #指定輪詢幾率,weight和訪問比率成正比,用於后端服務器性能不均的情況。
163         #例如:
164         #upstream bakend {
165         #    server 192.168.0.14 weight=10;
166         #    server 192.168.0.15 weight=10;
167         #}
168         #2、ip_hash
169         #每個請求按訪問ip的hash結果分配,這樣每個訪客固定訪問一個后端服務器,可以解決session的問題。
170         #例如:
171         #upstream bakend {
172         #    ip_hash;
173         #    server 192.168.0.14:88;
174         #    server 192.168.0.15:80;
175         #}
176         #3、fair(第三方)
177         #按后端服務器的響應時間來分配請求,響應時間短的優先分配。
178         #upstream backend {
179         #    server server1;
180         #    server server2;
181         #    fair;
182         #}
183         #4、url_hash(第三方)
184         #按訪問url的hash結果來分配請求,使每個url定向到同一個后端服務器,后端服務器為緩存時比較有效。
185         #例:在upstream中加入hash語句,server語句中不能寫入weight等其他的參數,hash_method是使用的hash算法
186         #upstream backend {
187         #    server squid1:3128;
188         #    server squid2:3128;
189         #    hash $request_uri;
190         #    hash_method crc32;
191         #}
192  
193         #tips:
194         #upstream bakend{#定義負載均衡設備的Ip及設備狀態}{
195         #    ip_hash;
196         #    server 127.0.0.1:9090 down;
197         #    server 127.0.0.1:8080 weight=2;
198         #    server 127.0.0.1:6060;
199         #    server 127.0.0.1:7070 backup;
200         #}
201         #在需要使用負載均衡的server中增加 proxy_pass http://bakend/;
202  
203         #每個設備的狀態設置為:
204         #1.down表示單前的server暫時不參與負載
205         #2.weight為weight越大,負載的權重就越大。
206         #3.max_fails:允許請求失敗的次數默認為1.當超過最大次數時,返回proxy_next_upstream模塊定義的錯誤
207         #4.fail_timeout:max_fails次失敗后,暫停的時間。
208         #5.backup: 其它所有的非backup機器down或者忙的時候,請求backup機器。所以這台機器壓力會最輕。
209  
210         #nginx支持同時設置多組的負載均衡,用來給不用的server來使用。
211         #client_body_in_file_only設置為On 可以講client post過來的數據記錄到文件中用來做debug
212         #client_body_temp_path設置記錄文件的目錄 可以設置最多3層目錄
213         #location對URL進行匹配.可以進行重定向或者進行新的代理 負載均衡
214     }
215     
216     #虛擬主機的配置
217     server {
218         #監聽端口
219         listen       80;
220         #域名可以有多個,用空格隔開
221         server_name  mmzsblog.cn;
222         index index.html index.htm index.php;
223         #root /data/www/jd;
224                 
225         #對 "/" 啟用反向代理
226         location / {
227             add_header X-Static transfer;
228 
229             #如果是本機就用localhost
230             proxy_pass http://mmzsblog.cn;
231             proxy_redirect off;
232             proxy_set_header X-Real-IP $remote_addr;
233 
234 
235             #以下是一些反向代理的配置,可選。
236             proxy_set_header Host $host;
237 
238             #后端的Web服務器可以通過X-Forwarded-For獲取用戶真實IP
239             proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
240 
241             #允許客戶端請求的最大單文件字節數
242             client_max_body_size 10m;
243 
244             #緩沖區代理緩沖用戶端請求的最大字節數,
245             #如果把它設置為比較大的數值,例如256k,那么,無論使用firefox還是IE瀏覽器,來提交任意小於256k的圖片,都很正常。如果注釋該指令,使用默認的client_body_buffer_size設置,也就是操作系統頁面大小的兩倍,8k或者16k,問題就出現了。
246             #無論使用firefox4.0還是IE8.0,提交一個比較大,200k左右的圖片,都返回500 Internal Server Error錯誤
247             client_body_buffer_size 128k;
248 
249             #表示使nginx阻止HTTP應答代碼為400或者更高的應答。
250             proxy_intercept_errors on;
251 
252             #后端服務器連接的超時時間_發起握手等候響應超時時間
253             #nginx跟后端服務器連接超時時間(代理連接超時)
254             proxy_connect_timeout 90;
255 
256             #后端服務器數據回傳時間(代理發送超時)
257             #后端服務器數據回傳時間_就是在規定時間之內后端服務器必須傳完所有的數據
258             proxy_send_timeout 90;
259 
260             #連接成功后,后端服務器響應時間(代理接收超時)
261             #連接成功后_等候后端服務器響應時間_其實已經進入后端的排隊之中等候處理(也可以說是后端服務器處理請求的時間)
262             proxy_read_timeout 90;
263 
264             #設置代理服務器(nginx)保存用戶頭信息的緩沖區大小
265             #設置從被代理服務器讀取的第一部分應答的緩沖區大小,通常情況下這部分應答中包含一個小的應答頭,默認情況下這個值的大小為指令proxy_buffers中指定的一個緩沖區的大小,不過可以將其設置為更小
266             proxy_buffer_size 4k;
267 
268             #proxy_buffers緩沖區,網頁平均在32k以下的設置
269             #設置用於讀取應答(來自被代理服務器)的緩沖區數目和大小,默認情況也為分頁大小,根據操作系統的不同可能是4k或者8k
270             proxy_buffers 4 32k;
271 
272             #高負荷下緩沖大小(proxy_buffers*2273             proxy_busy_buffers_size 64k;
274 
275             #設置在寫入proxy_temp_path時數據的大小,預防一個工作進程在傳遞文件時阻塞太長
276             #設定緩存文件夾大小,大於這個值,將從upstream服務器傳
277             proxy_temp_file_write_size 64k;
278         }
279         
280         #對******進行負載均衡
281         location ~ .*.(php|php5)?$
282         {
283             fastcgi_pass 127.0.0.1:9000;
284             fastcgi_index index.php;
285             include fastcgi.conf;
286         }
287          
288         #圖片緩存時間設置
289         location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$
290         {
291             expires 10d;
292         }
293          
294         #JS和CSS緩存時間設置
295         location ~ .*.(js|css)?$
296         {
297             expires 1h;
298         }
299          
300         #日志格式設定
301         #$remote_addr與$http_x_forwarded_for用以記錄客戶端的ip地址;
302         #$remote_user:用來記錄客戶端用戶名稱;
303         #$time_local: 用來記錄訪問時間與時區;
304         #$request: 用來記錄請求的url與http協議;
305         #$status: 用來記錄請求狀態;成功是200,
306         #$body_bytes_sent :記錄發送給客戶端文件主體內容大小;
307         #$http_referer:用來記錄從那個頁面鏈接訪問過來的;
308         #$http_user_agent:記錄客戶瀏覽器的相關信息;
309         #通常web服務器放在反向代理的后面,這樣就不能獲取到客戶的IP地址了,通過$remote_add拿到的IP地址是反向代理服務器的iP地址。反向代理服務器在轉發請求的http頭信息中,可以增加x_forwarded_for信息,用以記錄原有客戶端的IP地址和原來客戶端的請求的服務器地址。
310         log_format access '$remote_addr - $remote_user [$time_local] "$request" '
311         '$status $body_bytes_sent "$http_referer" '
312         '"$http_user_agent" $http_x_forwarded_for';
313 
314         #css|js|ico|gif|jpg|jpeg|png|txt|html|htm|xml|swf|wav這些都是靜態文件,但應分辨,js、css可能經常會變,過期時間應小一些,圖片、html基本不變,過期時間可以設長一些
315         location ~* ^.+\.(ico|gif|jpg|jpeg|png|html|htm)$ {
316             root         html;
317             access_log   logs/access.log;
318             expires      -1s;
319         }
320 
321         gzip on;
322         gzip_comp_level 7;
323         gzip_min_length  1100; #需要壓縮的最小長度
324         gzip_buffers    4 8k;
325         gzip_types      text/plain application/javascript text/css text/xml application/x-httpd-php; #指定需要壓縮的文件類型
326         output_buffers  1 32k;
327         postpone_output  1460;
328  
329         #error_page  404              /404.html;
330         # redirect server error pages to the static page /50x.html
331         error_page   500 502 503 504  /50x.html;
332         location = /50x.html {
333             root   html;
334         }
335         
336         #設定查看Nginx狀態的地址
337         location /NginxStatus {
338             stub_status on;
339             access_log on;
340             auth_basic "NginxStatus";
341             auth_basic_user_file confpasswd;
342             #htpasswd文件的內容可以用apache提供的htpasswd工具來產生。
343         }
344          
345         #本地動靜分離反向代理配置
346         #所有jsp的頁面均交由tomcat或resin處理
347         location ~ .(jsp|jspx|do)?$ {
348             proxy_set_header Host $host;
349             proxy_set_header X-Real-IP $remote_addr;
350             proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
351             proxy_pass http://127.0.0.1:8080;
352         }
353          
354         #所有靜態文件由nginx直接讀取不經過tomcat或resin
355         location ~ .*.(htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|
356         pdf|xls|mp3|wma)$
357         {
358             expires 15d; 
359         }
360          
361         location ~ .*.(js|css)?$
362         {
363             expires 1h;
364         }
365         
366     }
367 }
/software/nginx-1.12.2/conf/nginx.conf

 

配置完成后測試是否正常:

/usr/local/nginx/sbin/nginx -t  

d、啟動ngnix

記得要啟動tomcat;

/usr/local/nginx/sbin/nginx

若修改后配置文件或者將配置文件覆蓋修改的 ,需要執行:

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

修改后需要重啟nginx,發現重啟不了端口被占用,則用一下命令解決:

netstat  grep 80   --查看端口80占用

sudo fuser -k 80/tcp   --關閉端口程序,然后重啟即可

關閉Nginx:

/usr/local/nginx/sbin/nginx -s stop

重啟Nginx:

/usr/local/nginx/sbin/nginx

查看nginx是否監聽了80端口:

netstat -tnlp

 

 

5、可能出現的問題

a、訪問錯誤

【錯誤原因:】訪問太頻繁,瀏覽器的緩存量太大,產生錯誤。

【解決辦法:】清理瀏覽器的cookie記錄,和緩存文件,重啟瀏覽器就好了。

b、配置錯誤

nginx: [emerg] "client_header_buffer_size" directive is not allowed here in /usr/local/nginx/conf/nginx.conf:51
nginx: configuration file /usr/local/nginx/conf/nginx.conf test failed

【錯誤原因:】url頭過長

【解決辦法:】在/software/nginx-1.12.2/conf/nginx.conf中自己修改對應配置

c、配置沒生效

安裝nginx后,會自動生成目錄/usr/local/nginx;

卸載時記得將它刪除,否則重新配置時是不起作用的。

d、啟動ngnix,發現報錯

error while loading shared libraries: libpcre.so.1: cannot open shared object file: No such file or directory

經網上查詢,這是linux的通病

[root@localhost nginx]# sbin/nginx 
sbin/nginx: error while loading shared libraries: libpcre.so.1: cannot open shared object file: No such file or directory
[root@localhost nginx]# error while loading shared libraries: libpcre.so.1: cannot open shared object file: No such file or directory
[root@localhost nginx]# whereis libpcre.so.1
libpcre.so: /lib64/libpcre.so.0 /usr/local/lib/libpcre.so /usr/local/lib/libpcre.so.1
[root@localhost nginx]# ln -s /usr/local/lib/libpcre.so.1 /lib64
[root@localhost nginx]# sbin/nginx   


參考文章:

1、http://27wy.cn/archives/78

2、http://27wy.cn/archives/80

 

[root@localhost ~] 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM