用Nginx配置二级域名多个应用服务项目等

可以解决的问题:

1、一台服务器可以起多个tomcat通过不同的端口,共同监听80端口映射到不通的域名上。

2、微信服务器多个应用,需要多个域名的,可以通过nginx进行转发,对不通的服务器进行微信授权。

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

进去,cd  vhost,然后创建两个XX.conf 、XXX.conf

辅助配置位置

Nginx主配置文件

user www www;
worker_processes 1; 
error_log /usr/local/work/nginx/logs/nginx_error.log crit; 
pid /usr/local/work/nginx/nginx.pid;
#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 65535;
events
{
  use epoll;
  worker_connections 65535;
}
http
{
  include mime.types;
  default_type application/octet-stream;
  log_format main  '$remote_addr - $remote_user [$time_local] "$request" '
               '$status $body_bytes_sent "$http_referer" '
               '"$http_user_agent" $http_x_forwarded_for';
  
#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;
  fastcgi_connect_timeout 300;
  fastcgi_send_timeout 300;
  fastcgi_read_timeout 300;
  fastcgi_buffer_size 64k;
  fastcgi_buffers 4 64k;
  fastcgi_busy_buffers_size 128k;
  fastcgi_temp_file_write_size 128k;
  gzip on; 
  gzip_min_length 1k;
  gzip_buffers 4 16k;
  gzip_http_version 1.0;
  gzip_comp_level 2;
  gzip_types text/plain application/x-javascript text/css application/xml;
  gzip_vary on;
 
  #limit_zone crawler $binary_remote_addr 10m;

#这里加上辅助配置的位置 也可以直接写绝对路径
  include   vhost/*.conf;
  
   server
  {
    listen 80;
    server_name localhost;
    index index.html index.htm index.php;
    root /usr/local/work/nginx/html;
      location ~ .*\.(php|php5)?$
    {
      #fastcgi_pass unix:/tmp/php-cgi.sock;
      fastcgi_pass 127.0.0.1:9000;
      fastcgi_index index.php;
      include fastcgi.conf;
    }
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|ico)$
    {
      expires 30d;
  # access_log off;
    }
    location ~ .*\.(js|css)?$
    {
      expires 15d;
   # access_log off;
    }
    access_log off;
#这里加上项目启动后显示的信息 / ,及错误页面
	if ( $host ~* (\b(?!www\b)\w+)\.\w+\.\w+ ) {
 set $subdomain /$1;
}
location / {
 root html/$subdomain;
 index index.html index.php index.htm index;
}
#
  }

}

Nginx辅助配置文件-1

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

   server {
   #**全部都是监听的80端口进行不同的映射哦**
        listen       80;
   #这里需要配置二级域名
        server_name  二级域名;
        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://www.baidu.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";
           }

--------------------- 
作者:alen_en 
来源:CSDN 
原文:https://blog.csdn.net/alen_en/article/details/78890287 
版权声明:本文为博主原创文章,转载请附上博文链接!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值