Nginx基本配置和作用


nginx可以重新加載文件的。我們直接運行:nginx -s reload 

配置文件有沒有問題,可以直接輸入:nginx -t

nginx -s stop就可以關閉

但有時我們就不想它掛的時候訪問另外一個,而只是希望一個服務器訪問的機會比另外一個大,使用weight

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#gzip  on;
 
     #一個服務器掛了,多配置一個jetty,weight=數字來指定,數字越大,表明請求到的機會越大
 
     upstream local_tomcat { 
             server localhost: 8080  weight= 1 ;
             server localhost: 9999  weight= 5 ;
     server{
         listen        8030 ;
         server_name  localhost: 8080 ;
         #charset koi8-r;
        #access_log  logs/host.access.log  main;
         location / {proxy_pass http: //local_tomcat;} 
         ##......其他省略
         #過濾不同的jsp和靜態html頁面
         #location / {  proxy_pass http: //localhost:8080;}
         #location ~ .jsp$ {    proxy_pass http: //localhost:8080;} 
         #location ~ .(html|js|css|png|gif)$ {  root D:/apache-tomcat- 7.0 . 77 /webapps/ROOT;}
         error_page    500  502  503  504   /50x.html;
 
         location = /50x.html {
 
             root   html;
 
         }
 
     }
<wiz_tmp_tag class="wiz-block-scroll">
 

1、nginx能做反向代理,那么什么是反向代理呢,舉個栗子,我想在本地使用 www.mickey.com 的域名去訪問 www.taobao.com。那么這個時候我們就可以通過nginx去實現。

2、nginx能實現負載均衡,什么是負載均衡呢?就是我的項目部署在不同的服務器上,但是通過統一的域名進入,nginx則對請求進行分發,減輕了服務器的壓力。

3、作為安全隔離的作用;

4、解決跨域問題。

5、緩存靜態文件,加快訪問速度。

 

修改nginx.conf 配置文件,啟用 upstream 負載均衡 tomcat Cluster,默認使用輪詢方式。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
upstream site { 
     server localhost: 8080 ;
     server localhost: 9090 ;
 
server {
     listen        80 ;
     server_name  localhost;
 
     #charset koi8-r;
     #access_log  /var/log/nginx/log/host.access.log  main;
 
     location / {
         #root   /usr/share/nginx/html;
         #index  index.html index.htm;
         index  index_tel.jsp index.jsp index.html index.htm ; 
         proxy_redirect          off;   
         proxy_set_header        Host            $host;   
         proxy_set_header        X-Real-IP       $remote_addr;   
         proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;   
         client_max_body_size    10m;   
         client_body_buffer_size 128k;   
         proxy_buffers            32  4k; 
         proxy_connect_timeout    3 ;   
         proxy_send_timeout       30 ;   
         proxy_read_timeout       30 ;  
             proxy_pass http: //site;
 
     }
<wiz_tmp_tag class="wiz-block-scroll">
 

 

測試:

 

1、訪問 http://10.129.221.70:8080 直接請求到tomcat_1服務器,顯示 “ response from tomcat_1 ”, session 值為 ‘56E2FAE376A47F1C0961D722326B8423’;

2、訪問 http://10.129.221.70:9090 直接請求到tomcat_2服務器,顯示 “ response from tomcat_2 ”, session 值為 ‘56E2FAE376A47F1C0961D722326B8423’;

3、訪問 http://10.129.221.70 (默認80端口)請求到 nginx 反向代理到指定Web服務器,由於默認使用輪詢負載方式,反復刷新頁面顯示的內容在“ response from tomcat_1 ” 和 “ response from tomcat_2 ”之間切換,但 session 值保持為 ‘56E2FAE376A47F1C0961D722326B8423’;

4、使用 redis-cli 連接 redis 服務器,查看會顯示有 “56E2FAE376A47F1C0961D722326B8423” key的 session 數據,value為序列化數據。

 

 


免責聲明!

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



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