Nginx服务器使用多域名配置多个网站

1.配置思路

通过给不同的域名配置不同的反向代理实现将域名指向不同的端口,分别用Express在3000端口与3001端口代理静态Vue网站即可实现在一台服务器上部署多个Vue项目

user                 nobody;
worker_processes     1;
worker_rlimit_nofile 65535;

#error_log  logs/error.log  notice;

events {
    accept_mutex off;
    use epoll;
    worker_connections  8192;
}

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

    log_format       combinedio  '$remote_addr - $remote_user [$time_local] '
                                 '"$request" $status $body_bytes_sent '
                                 '"$http_referer" "$http_user_agent" $request_length $request_time $upstream_response_time';
    access_log logs/access.log combinedio;

    sendfile                     on;
    gzip                         on;
    tcp_nopush                   on;
    tcp_nodelay		         on;

    keepalive_timeout            0;
    client_body_timeout          10;
    client_header_timeout        10;

    client_header_buffer_size    1k;
    large_client_header_buffers  4  4k;
    output_buffers               2  32k;
    client_max_body_size	 64m;
    client_body_buffer_size      256k; 

    server_tokens off;

    include http.d/*.conf;
 
    server {
        listen       80;
        server_name  test.demo.club;


        location / {
	    add_header 'Access-Control-Allow-Origin' '*';
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
        add_header 'Access-Control-Allow-Headers' 'DNT,web-token,app-token,Authorization,Accept,Origin,Keep-Alive,User-Agent,X-Mx-ReqToken,X-Data-Type,X-Auth-Token,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
        add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
	    proxy_pass	http://127.0.0.1:3000;
        }

    }
    server {
      listen  80;
      server_name www.demo.club;

      location / {
        proxy_pass  http://127.0.0.1:3001;
      }
    }
    # include include/*.conf;
}

具体Nginx.conf如上

2.遇到的问题

最初思路并不是现在的实现方法,而是计划使用81端口反向代理至3001端口实现访问81端口来跳转第二页面,但实际情况是并不能通过域名:81的形式直接访问到81端口,因此此方式失败

另一种思路是通过配置路由来分配不同反向代理,如

# 配置重定向到/usr/financial
    #     location ^~/ifinancial {
    #   root /usr/financial;
    #   index index.html;
    #   rewrite ^/ifinancial/(.*)\.*$ /ifinancial/$1 break;
    #   try_files $uri $uri/ /ifinancial/index.html;
    # }
但这种方式需要在Vue配置文件中同步更改根路由,同时网上只给出路由模式使用History时的解决方案,因此在多次尝试后舍弃了此种思路
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值