如何使用2 React App和Node Server配置NGINX子域?

时间:2018-10-20 20:11:17

标签: node.js reactjs nginx

我是NGINX的初学者,我的问题是我有1个域和2个子域。我的OS服务器是Ubuntu 16.04

  1. www。 + myDomain.com,此dns应该定向到http://locolhost:5000(反应应用程序)
  2. api。 + myDomain.com,此dns应该定向到http://locolhost:6000(节点服务器)

这是我的配置代码

server {
    listen 80;
    listen [::]:80;
    server_name www.myDomain.com;
    return 301 https://$server_name$request_uri;
}
#config node server
server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    ssl_certificate /etc/ssl/certs/localhost.crt;
    ssl_certificate_key /etc/ssl/private/localhost.key;

    ssl_protocols TLSv1.2 TLSv1.1 TLSv1;

    root /root/;

    server_name https://api.myDomain.com/;

    location / {
        # node server 
        proxy_pass http://localhost:6000;
        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;
    }

    location /blog {
        try_files $uri $uri/ /index.php$is_args$args;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    }

    location ~ /\.ht {
        deny all;
    }
}

#config react app
server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    ssl_certificate /etc/ssl/certs/localhost.crt;
    ssl_certificate_key /etc/ssl/private/localhost.key;

    ssl_protocols TLSv1.2 TLSv1.1 TLSv1;

    root /root/;


    server_name https:// + www.myDomain.com/;

    location / {
        proxy_pass http://localhost:5000;
        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;
    }

    location /blog {
        try_files $uri $uri/ /index.php$is_args$args;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    }

    location ~ /\.ht {
        deny all;
    }
}

当我访问www.myDomain.com时,他们重定向到可正常运行的React应用(localhost:5000)

但是当我转到api.myDomain.com时,它们重定向到与www.myDomain.com相同的地址,我希望我得到的结果应该是节点服务器(localhost:6000)

我坚持了几天,请帮助我。      非常感谢您的帮助,对于我的英语T-T水平不好,我深表歉意

0 个答案:

没有答案