用Nginx配置二级域名多个微信服务应用等

可以解决的问题:

  1. 一台服务器可以起多个tomcat通过不同的端口,共同监听80端口映射到不通的域名上。
  2. 微信服务器多个应用,需要多个域名的,可以通过nginx进行转发,对不通的服务器进行微信授权。

nginx文件夹(就是nginx.conf所在位置)同级建一个vhost里面放辅助的配置文件

这里写图片描述

辅助配置位置
这里写图片描述

Nginx主配置文件

worker_processes  4;

events {
    use epoll;
    worker_connections  102400;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" $http_host '
                      '$status $upstream_status  $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" $upstream_addr '
                      '$request_time $upstream_response_time '
                      '"$http_x_forwarded_for"';
    access_log  logs/access.log  main;

    sendfile        on;
    tcp_nopush     on;


    keepalive_timeout  60s;
 max_size=2048m inactive=60m;


    fastcgi_connect_timeout 3000;
    fastcgi_send_timeout 3000;
    fastcgi_read_timeout 3000;
    fastcgi_buffer_size 256k;
    fastcgi_buffers 8 256k;
    fastcgi_busy_buffers_size 256k;
    fastcgi_temp_file_write_size 256k;
    fastcgi_intercept_errors on;

    client_header_timeout 600s;
    client_body_timeout 600s;
   #client_max_body_size 50m;
    client_max_body_size 100m;              
    client_body_buffer_size 256k;           

    gzip  on;
    gzip_min_length  1k;
    gzip_buffers     4 16k;
    gzip_http_version 1.1;
    gzip_comp_level 9;
    gzip_types       text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php;
    gzip_vary on;
    #这里配置辅助配置文件的位置
    include   vhost/*.conf;

}

Nginx辅助配置文件-1

#二级域名
  upstream xxxx.xxxx.com{
        ip_hash;
        #**这里配置你需要的服务器ip地址和端口号 也可以是一台服务器的多个端口进行不同的映射**
            server xxx.xx.xx.xxx:8088 max_fails=3 fail_timeout=30s;
    }

   server {
   #**全部都是监听的80端口进行不同的映射哦**
        listen       80;
   #这里需要配置二级域名
        server_name  xxxx.xxxx.com;
        access_log off;
        large_client_header_buffers 4 16k;
        client_max_body_size 3000m;
        client_body_buffer_size 128k;
        proxy_read_timeout 600s;

        resolver_timeout 600s;
        client_body_timeout 600s;
        client_header_timeout 600s;

         proxy_redirect off ;
         proxy_set_header Host $host;
         proxy_set_header X-Real-IP $remote_addr;
         proxy_set_header REMOTE-HOST $remote_addr;
         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
         proxy_connect_timeout 300;            
         proxy_send_timeout 300;               
         proxy_buffer_size 256k;               
         proxy_buffers 4 256k;                 
         proxy_busy_buffers_size 256k;          
         proxy_temp_file_write_size 256k;       
         proxy_next_upstream error timeout invalid_header http_500 http_503 http_404;
         proxy_max_temp_file_size 128m;

         location ~^/.* {
          #这里需要配置二级域名
        proxy_pass http://xxxx.xxxx.com;
        }

       error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

    }

Nginx辅助配置文件-2

#二级域名
  upstream xx.xx.com{
        ip_hash;
        #**这里配置你需要的服务器ip地址和端口号 也可以是一台服务器的多个端口进行不同的映射**
            server xxx.xx.xx.xxx:9099 max_fails=3 fail_timeout=30s;
    }

   server {
   #**全部都是监听的80端口进行不同的映射哦**
        listen       80;
   #这里需要配置二级域名
        server_name  xx.xx.com;
        access_log off;
        large_client_header_buffers 4 16k;
        client_max_body_size 3000m;
        client_body_buffer_size 128k;
        proxy_read_timeout 600s;

        resolver_timeout 600s;
        client_body_timeout 600s;
        client_header_timeout 600s;

         proxy_redirect off ;
         proxy_set_header Host $host;
         proxy_set_header X-Real-IP $remote_addr;
         proxy_set_header REMOTE-HOST $remote_addr;
         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
         proxy_connect_timeout 300;            
         proxy_send_timeout 300;               
         proxy_buffer_size 256k;               
         proxy_buffers 4 256k;                 
         proxy_busy_buffers_size 256k;          
         proxy_temp_file_write_size 256k;       
         proxy_next_upstream error timeout invalid_header http_500 http_503 http_404;
         proxy_max_temp_file_size 128m;

         location ~^/.* {
          #这里需要配置二级域名
        proxy_pass http://xx.xx.com;
        }

       error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

    }

这个是配置微信授权多个应用的nginx配置,映射到二级域名上
微信的授权:weixin.com 或者 weixin.com/xxx
才可以进行授权,所以要使用nginx进行转换才可以映射到你的二级域名上

xxx是你的微信公众号的主域名下的关键词,例如 weixin.com/xxx
 location /xxx/ {    
 #二级域名的配置
              proxy_pass  http://xxxxx.xxxxx.com/;
               proxy_redirect off;
               proxy_set_header  X-Real-IP  $remote_addr;
               #proxy_cache cache;
               #proxy_cache_valid 200 302 1d;
               #proxy_cache_valid 301 1d;
               #proxy_cache_valid any 1m;
               expires 1h;
                #二级域名的配置,注意后面的/  一定要加上
               add_header Nginx-Res "http://xxxxx.xxxxx.com/";
               proxy_ignore_headers "Cache-Control" "Expires" "Set-Cookie";
               #add_header Nginx-Cache "$upstream_cache_status";
           }

猜你喜欢

转载自blog.csdn.net/alen_en/article/details/78890287