Nginx配置相同端口号访问多个工程,多个工程在不同的tomcat,而且访问路径不同,特此记录一下配置过程。

upstream server33068 {
    server 172.16.0.10:33068;
}

upstream server33069 {
    server 172.16.0.10:33069;
}

server {
        listen       33060;
        server_name  localhost;
        access_log  /usr/share/nginx/html/test/erphrjpre.log  main;
        error_log   /usr/share/nginx/html/test/erphrjpre.error;
        client_max_body_size 60M;
        client_body_buffer_size 512k;

        location /erphrjpre {
            alias  /usr/share/nginx/html/erphrjpre;
            index  index.html;
        }

    location / {
        proxy_pass      http://server33068/;
        proxy_redirect  off;
        proxy_send_timeout 300;
        proxy_read_timeout 300;
        proxy_connect_timeout 300;

        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
     }

  location /v1/ {
                proxy_pass      http://server33068/;
                proxy_redirect  off;
                proxy_send_timeout 300;
                proxy_read_timeout 300;
                proxy_connect_timeout 300;

                proxy_set_header Host $host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  }


     location /v2/ {
         proxy_pass   http://server33069/;
     }
 
}

 

这里需要注意的是,"/v1/"、"/v2/"中最后的斜杠"/",以及"http://server33068/"、"http://server33069/"中最后的斜杠"/"都是不能去掉的,否则访问会有问题。

 

url访问地址:
http://106.12.*.140:33060/api/sys/ping http://106.12.*.140:33060/v1/api/sys/ping http://106.12.*.140:33060/v2/api/sys/ping