用于节点应用程序的Nginx反向代理子域

时间:2015-12-16 17:36:31

标签: node.js nginx proxy subdomain

我对NGINX配置很新。我查看了许多帖子,寻找遇到此问题的其他人并找到了可行的解决方案。

我有2个node.js网络应用程序,它们将在2个不同的端口上运行(例如81, 82)。

我的初衷是使用反向代理,以便访问者可以访问相同的物理盒,但根据他们使用的域提供不同的内容。

我成功地根据他们的域分离了网站渲染。其中一个应用程序有一个与之关联的子域(app.exampleb.com),似乎每当我尝试访问该子域时,nginx都会为我提供一个页面,说明“我已经成功配置了服务器”.....谢谢nginx。

我已将以下内容放在我的nginx.conf文件中:

http {

    ##
    # Basic Settings
    ##

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    # server_tokens off;

    # server_names_hash_bucket_size 64;
    # server_name_in_redirect off;

    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    ##
    # Logging Settings
    ##

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    ##
    # Gzip Settings
    ##

    gzip on;
    gzip_disable "msie6";

    # gzip_vary on;
    # gzip_proxied any;
    # gzip_comp_level 6;
    # gzip_buffers 16 8k;
    # gzip_http_version 1.1;
    # gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;

    ##
    # nginx-naxsi config
    ##
    # Uncomment it if you installed nginx-naxsi
    ##

    #include /etc/nginx/naxsi_core.rules;

    ##
    # nginx-passenger config
    ##
    # Uncomment it if you installed nginx-passenger
    ##

    #passenger_root /usr;
    #passenger_ruby /usr/bin/ruby;

    server {
        listen       80;
        server_name  examplea.com;

         location / {
        proxy_pass http://localhost:81;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }
    }

    server {
            listen       80;
            server_name  app.examplea.com;

             location / {
        proxy_pass http://localhost:81;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }
        }


    server {
        listen       80;
        server_name  exampleb.com;

        location / {
         proxy_pass http://localhost:82;
        }
    }

    ##
    # Virtual Host Configs
    ##

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}

1 个答案:

答案 0 :(得分:0)

完整配置应为:

server {
    listen 80;

    server_name example.com;

    location / {
        proxy_pass http://localhost:81;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }
}

将其放在http {}

中的nginx.conf指令内

请务必重新启动nginx