Nginx (發音同 engine x)是一款輕量級的Web 服務器/反向代理服務器及電子郵件(IMAP/POP3)代理服務器,並在一個BSD-like 協議下發行。 其特色是佔有內存少,併發能力強,事實上nginx的併發能力確實在同類型的網頁伺服器中表現較好。目前中國大陸使用nginx網站用戶有:新浪、網易、 騰訊,另外知名的微網誌Plurk也使用nginx。javascript
工做原理圖以下:php
tomcat服務器咱們能夠準備二、3個tomcat服務器進行測試。css
安裝就很少說了(不會的看這),直接看配置:html
依次修改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" /> ...
沒有配置全局變量就這樣子啓動和關閉:java
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。node
一、 jdk 1.8.0_102(不會安裝的看這裏)linux
二、nginx 1.12.0(在官網上下一個解壓就行,官網:http://nginx.org/);nginx
三、2個或多個tomcat 6.x 7.x 8.x 9.x 均可以(好比我準備的是3個如出一轍的8.x版本,只是配置文件改了而已,一會會詳細說怎麼改 ,官網:http://tomcat.apache.org/)c++
yum install gcc
yum install gcc-c++ yum install prce pcre-devel yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel
下載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)
安裝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 instal
安裝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
配置路徑在 /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 }
帶中文詳細註解配置,可根據須要自行修改:
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.0) 127 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*2) 273 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 }
配置完成後測試是否正常:
/usr/local/nginx/sbin/nginx -t
記得要啓動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 -s relo
查看nginx是否監聽了80端口:
netstat -tnlp
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
參考文章:
一、http://27wy.cn/archives/78
二、http://27wy.cn/archives/80