前提條件:javascript
(1)已安裝好tomcat,且能成功啓動css
(2)已安裝好Nginx,且能成功啓動html
接下來進行配置:java
(1)在Nginx的conf文件夾中新增兩個文件,分別以下:(新建文件後,直接複製代碼便可)tomcat
文件1:Proxy.confapp
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_connect_timeout 300; proxy_send_timeout 300; proxy_read_timeout 300; proxy_buffer_size 4k; proxy_buffers 4 32k; proxy_busy_buffers_size 64k; proxy_temp_file_write_size 64k; gzip on; gzip_min_length 1000; gzip_types text/plain text/css application/x-javascript;
文件2:Gzip.confide
gzip on; gzip_min_length 1000; gzip_types text/plain text/css application/x-javascript;
(2)修改Nginx.conf文件,在http{ 新增代碼(以下) }:(注:tomcat的開放端口提早設爲12337,下面代碼用Nginx的8085來代理)spa
include gzip.conf; upstream localhost { #ip_hash #ip_hash; server localhost:12337; } server { listen 8085; server_name localhost; location / { proxy_connect_timeout 3; proxy_send_timeout 30; proxy_read_timeout 30; proxy_pass http://localhost; } }
(3)若是要設置tomcat動靜分離,使得訪問靜態頁面不須要進入tomcat代理
好比:將靜態頁面所存放的文件夾命名爲static,路徑爲E:/staticcode
那麼將步驟(2)新增的代碼作以下處理,添加代碼以下:
include gzip.conf; upstream localhost { #ip_hash #ip_hash; server localhost:12337; # server localhost:12111; #可配置多個端口 } server { listen 8085; server_name localhost; #設定訪問靜態文件直接讀取不通過tomcat location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ { root E:\static; expires 30d; } location ~ .*\.(js|css)?$ { root E:\static; expires 1h; } location / { proxy_connect_timeout 3; proxy_send_timeout 30; proxy_read_timeout 30; proxy_pass http://localhost; } }
配置完畢後,啓動tomcat,再啓動Nginx
最後效果:訪問127.0.0.1:8085 和 127.0.0.1:12337 頁面的內容同樣。
參考連接: