nginx、tomcat實現動靜分離,反向代理

假設有三臺服務器  192.168.23.11  192.168.23.12  192.168.23.13

 192.168.23.11 作爲nginx服務器  

192.168.23.12  192.168.23.13作爲tomcat服務器

首先安裝nginx的環境

配置nginx.conf文件

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    gzip  on;
    upstream mywork {
        server 192.168.23.12  weight=1;
		server 192.168.23.13  weight=1;
    }

    server {
        listen       80;
        server_name  mywork;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;
         location / {
          root work;
          index index.html;
          }        
         location  ~ .*\.(pdf|gif|jpg|jpeg|bmp|png|ico|txt|js|css|ttf|woff)$ {
           expires 1d;
           root work;
          index index.html;
          }   

         location /tata {
            proxy_pass     http://mywork;
            proxy_set_header   X-Real-IP $remote_addr;
         }
         
         location /nginx_status {
          stub_status on;
         access_log off;
         allow all;
         }
           
 
        error_page  404              /404.html;

    
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        
    }


 
}

靜態的css/js/img等文件存在在nginx服務器

只有需要tomcat的纔去請求tomcat服務器


路徑要匹配

192.168.23.11/index.html是網站首頁

192.168.23.11/tata/是我們tomcat服務器的目錄

靜態文件的路徑需要在work文件中去匹配tata工程的路徑


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