nginx1.8.1配置反向代理

        上一篇文章介绍了如何安装nginx1.8.1,接下来介绍如何通过nginx1.8.1反向代理tomcat。

我的环境:

            本机是window系统    ip:192.168.1.131    tomcat端口:192.168.1.131:8085
            虚拟机是Ubuntu14.04系统  ip:192.168.1.150  nginx端口:80

反向代理结果:

            网址输入192.168.1.150就能看到192.168.1.131:8085页面

1:新建一个proxy.conf,路径和nginx.conf路径一样

sudo vim /usr/local/nginx/conf/proxy.conf

2 :把以下内容写入proxy.conf

#!nginx(-)
# proxy.conf
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr; #获取真实ip
#proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; #获取代理者的真实ip
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;

3:把以下内容写入nginx.conf

#运行nginx所在的用户名和用户组 
#user  www www;  

#启动进程数 
worker_processes 8; 
#全局错误日志及PID文件 
error_log  /usr/local/nginx/logs/nginx_error.log  crit; 

pid        /usr/local/nginx/nginx.pid; 

#Specifies the value for maximum file descriptors that can be opened by thisprocess. 

worker_rlimit_nofile 65535; 
#工作模式及连接数上限 
events 
{ 
  use epoll; 
  worker_connections 65535; 
} 
#设定http服务器,利用它的反向代理功能提供负载均衡支持 
http 
{ 
  #设定mime类型 
  include       mime.types; 
  default_type  application/octet-stream; 
  include /usr/local/nginx/conf/proxy.conf; 
  #charset  gb2312; 
  #设定请求缓冲     
  server_names_hash_bucket_size 128; 
  client_header_buffer_size 32k; 
  large_client_header_buffers 4 32k; 
  #client_max_body_size 8m; 

  sendfile on; 
  tcp_nopush     on; 

  keepalive_timeout 60; 

  tcp_nodelay on; 



      ###禁止通过ip访问站点 
  server{ 
        server_name _; 
        return 404; 
        } 


  server 
  { 
    listen       80; 
    server_name  localhost 127.0.0.1 192.168.1.150;  
    #rewrite ^/ http://192.168.1.131:8085;
    #limit_conn   crawler  20;     

    location ~ (\.jsp)|(\.do)|(\.js)|(\.css)|\.(gif|jpg|jpeg|png|bmp|swf)$ #所有jsp的页面均交由tomcat处理 
    {   
      proxy_pass http://192.168.1.131:8085;#转向tomcat处理
      proxy_redirect default; 
    } 

    location / 
    {
          proxy_pass http://192.168.1.131:8085;
    }

  }   
} 

你可能感兴趣的:(tomcat,nginx,ubuntu)