前提条件:
(1)已安装好tomcat,且能成功启动
(2)已安装好Nginx,且能成功启动
接下来进行配置:
(1)在Nginx的conf文件夹中新增两个文件,分别如下:(新建文件后,直接复制代码即可)
文件1:Proxy.conf
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;
Proxy.conf
文件2:Gzip.conf
gzip on; gzip_min_length 1000; gzip_types text/plain text/css application/x-javascript;
Gzip.conf
(2)修改Nginx.conf文件,在http{ 新增代码(如下) }:(注:tomcat的开放端口提前设为12337,下面代码用Nginx的8085来代理)
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:/static
那么将步骤(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 页面的内容一样。
参考链接:
http://www.cnblogs.com/super-d2/p/3662215.html
http://www.cnblogs.com/hughtxp/p/4323875.html
时间: 2024-10-30 15:01:47