nginx简单实现反向代理和静态资源服务器

1修改hosts文件

127.0.0.1    www.test1.com
127.0.0.1    www.test2.com
127.0.0.1    static.com

2配置tomcat的server.xml

<?xml version='1.0' encoding='utf-8'?>

<Server port="8005" shutdown="SHUTDOWN">
  <Listener className="org.apache.catalina.startup.VersionLoggerListener" />
  
  <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
  <Listener className="org.apache.catalina.core.JasperListener" />
  <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
  <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
  <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />

 
  <GlobalNamingResources>
   
    <Resource name="UserDatabase" auth="Container"
              type="org.apache.catalina.UserDatabase"
              description="User database that can be updated and saved"
              factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
              pathname="conf/tomcat-users.xml" />
  </GlobalNamingResources>

  
  <Service name="Catalina">

    <Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
    
    <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />

    <Engine name="Catalina" defaultHost="localhost">

      <Realm className="org.apache.catalina.realm.LockOutRealm">
        <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
               resourceName="UserDatabase"/>
      </Realm>

      <Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log." suffix=".txt"
               pattern="%h %l %u %t &quot;%r&quot; %s %b" />

      </Host>
      
       <Host name="www.test1.com"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">

        <Context path ="" docBase ="/test1" debug ="0" reloadbale ="true" ></Context>

      </Host>
      
      <Host name="www.test2.com"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">

        <Context path ="" docBase ="/test2" debug ="0" reloadbale ="true" ></Context>

      </Host>
    </Engine>
  </Service>
</Server>

将test1和test2项目放在tomcat根目录/webapps下(项目用war包或者war解压后的目录文件都行)

3配置nginx的nginx.conf文件

  1 #user  nobody;
  2 worker_processes  1;
  3 
  4 #error_log  logs/error.log;
  5 #error_log  logs/error.log  notice;
  6 #error_log  logs/error.log  info;
  7 
  8 #pid        logs/nginx.pid;
  9 
 10 
 11 events {
 12     worker_connections  1024;
 13 }
 14 
 15 
 16 http {
 17     include       mime.types;
 18     default_type  application/octet-stream;
 19 
 20     #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
 21     #                  '$status $body_bytes_sent "$http_referer" '
 22     #                  '"$http_user_agent" "$http_x_forwarded_for"';
 23 
 24     #access_log  logs/access.log  main;
 25 
 26     sendfile        on;
 27     #tcp_nopush     on;
 28 
 29     #keepalive_timeout  0;
 30     keepalive_timeout  65;
 31 
 32     #gzip  on;
 33 
 34     server {
 35         listen       80;
 36         server_name  localhost;
 37 
 38         #charset koi8-r;
 39 
 40         #access_log  logs/host.access.log  main;
 41 
 42         location / {
 43             root   html;
 44             index  index.html index.htm;
 45         }
 46 
 47         #error_page  404              /404.html;
 48 
 49         # redirect server error pages to the static page /50x.html
 50         #
 51         error_page   500 502 503 504  /50x.html;
 52         location = /50x.html {
 53             root   html;
 54         }
 55 
 56         # proxy the PHP scripts to Apache listening on 127.0.0.1:80
 57         #
 58         #location ~ .php$ {
 59         #    proxy_pass   http://127.0.0.1;
 60         #}
 61 
 62         # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
 63         #
 64         #location ~ .php$ {
 65         #    root           html;
 66         #    fastcgi_pass   127.0.0.1:9000;
 67         #    fastcgi_index  index.php;
 68         #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
 69         #    include        fastcgi_params;
 70         #}
 71 
 72         # deny access to .htaccess files, if Apache's document root
 73         # concurs with nginx's one
 74         #
 75         #location ~ /.ht {
 76         #    deny  all;
 77         #}
 78     }
 79 
 80 
 81     # another virtual host using mix of IP-, name-, and port-based configuration
 82     #
 83     #server {
 84     #    listen       8000;
 85     #    listen       somename:8080;
 86     #    server_name  somename  alias  another.alias;
 87 
 88     #    location / {
 89     #        root   html;
 90     #        index  index.html index.htm;
 91     #    }
 92     #}
 93 
 94 
 95     # HTTPS server
 96     #
 97     #server {
 98     #    listen       443 ssl;
 99     #    server_name  localhost;
100 
101     #    ssl_certificate      cert.pem;
102     #    ssl_certificate_key  cert.key;
103 
104     #    ssl_session_cache    shared:SSL:1m;
105     #    ssl_session_timeout  5m;
106 
107     #    ssl_ciphers  HIGH:!aNULL:!MD5;
108     #    ssl_prefer_server_ciphers  on;
109 
110     #    location / {
111     #        root   html;
112     #        index  index.html index.htm;
113     #    }
114     #}
115     
116     upstream Tomcat_client {
117        server localhost:8080;
118     }
119      server {
120         listen 80;
121         server_name www.test1.com;
122         
123         location / {
124             proxy_pass http://Tomcat_client;
125             proxy_redirect default;
126             #设置代理
127             proxy_set_header Host $host;
128             proxy_set_header X-Real-IP $remote_addr;
129         }
130     }
131     
132     server {
133         listen 80;
134         server_name www.test2.com;
135         location / {
136             proxy_pass http://Tomcat_client;
137             proxy_redirect default;
138             #设置代理
139             proxy_set_header Host $host;
140             proxy_set_header X-Real-IP $remote_addr;
141         }
142     }
143     
144     server {
145         listen 80;
146         server_name static.com;
147         
148         location / {
149             root   C:/image;
150             autoindex on;
151             
152         }
153     }
154 
155 }

主要关注116~153行;启动toncat和nginx,在C盘下建image文件,可通过http://static.com/直接访问静态资源。也可在其他html页面访问静态资源

浏览器输入www.test1.com回车,因为http默认端口为80,所以相当于输入的是www.test1.com:80 而nginx监听了80端口,找到server_name为www.test1.com的server,然后将请求交给被代理的tomcat服务器,如136行所示,Tomcat_client实际上表示localhost:8080

原文地址:https://www.cnblogs.com/ouym/p/7499056.html