【已解决】node nginx 反向多站点以及二级域名设置
发布于 10 年前 作者 heixiaoshan 22622 次浏览 最后一次编辑是 8 年前

/etc/nginx/nginx.conf

内容:

user nginx  nginx;
worker_processes 3;
error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;
events {
    worker_connections  1024;
}
http {
    include       /etc/nginx/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"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}

###/etc/nginx/conf.d/bz.conf (第一个网站配置文件)

server {
        listen 80;
    server_name www.bz.com bz.com bbs.bz.com;   //这个地方二级域名应该怎么设置呢 bbs.bz.com 是不同的node项目
    location / {
    proxy_pass http://127.0.0.1:4000;    //第二个子域名 http://127.0.0.1:4001
    proxy_set_header X-Real-IP ~Dremote_addr;
    proxy_set_header X-Forwarded-For ~Dproxy_add_x_forwarded_for;
    proxy_set_header Host ~Dhttp_host;
    proxy_set_header X-NginX-Proxy true;
    }
}


###/etc/nginx/conf.d/bg.conf (第二个网站配置文件)

server {
    listen 80;
    server_name www.bg.com bg.com;
    location / {
    proxy_pass http://127.0.0.1:3000;
    proxy_set_header X-Real-IP ~Dremote_addr;
   proxy_set_header X-Forwarded-For ~Dproxy_add_x_forwarded_for;
    proxy_set_header Host ~Dhttp_host;
    proxy_set_header X-NginX-Proxy true;
    }
}


###systemctl status nginx.service -l 命令报错(错误搞定)

nginx.service - nginx - high performance web server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled)
   Active: failed (Result: exit-code) since 二 2014-07-22 16:48:10 CST; 25min ago
     Docs: http://nginx.org/en/docs/
  Process: 2850 ExecStop=/bin/kill -s QUIT $MAINPID (code=exited, status=0/SUCCESS)
  Process: 2702 ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf (code=exited, status=0/SUCCESS)
  Process: 2883 ExecStartPre=/usr/sbin/nginx -t -c /etc/nginx/nginx.conf (code=exited, status=1/FAILURE)
 Main PID: 2705 (code=exited, status=0/SUCCESS)

7月 22 16:48:10 localhost.localdomain systemd[1]: Starting nginx - high performance web server...
7月 22 16:48:10 localhost.localdomain nginx[2883]: nginx: [emerg] a duplicate default server for 0.0.0.0:80 in /etc/nginx/conf.d/bz.conf:5
7月 22 16:48:10 localhost.localdomain nginx[2883]: nginx: configuration file /etc/nginx/nginx.conf test failed
7月 22 16:48:10 localhost.localdomain systemd[1]: nginx.service: control process exited, code=exited status=1
7月 22 16:48:10 localhost.localdomain systemd[1]: Failed to start nginx - high performance web server.
7月 22 16:48:10 localhost.localdomain systemd[1]: Unit nginx.service entered failed state.

####这个错误重启,注意防火墙一定要把端口加进去 ####最终排查是pm2的问题。有3个项目,pm2实际只成功启动2个,确显示3个。在最后发现问题的根源了。是express4 的配置问题,4分离了app端口,可进另一个帖子看解决方案【http://cnodejs.org/topic/53cffbcf691a430912542598】

10 回复

两个文件里都是 listen 80, 果然报错

@jiyinyiyong 搞定了,防火墙的问题

紧急啊

大神来看吧

别沉啊 @jiyinyiyong @youxiachai @leapon @suqian @a272121742 @leizongmin @panawang @alsotang @yakczh @JacksonTian 大神们啦,nginx二级域名设置啊 ,我现在用另一个配置文件 设置 w.XXX.com 直接报502错误,应该是没有映射到。

去nginx问好不。

@JacksonTian 不好。很纳闷,就没人弄过吗

@JacksonTian 我纳闷,最后发现是pm2的问题,pm2居然没有成功启动第三个app。但是list里显示成功的。我很纳闷

注意发贴格式 - -!

回到顶部