node.js - 我需要使用 nginx 服务器为 node.js 项目设置我的域

标签 node.js nginx subdomain digital-ocean ubuntu-18.04

我已经在我的服务器中添加了 CNAME 和 A 记录,我还使用 nginx 命令检查:

nginx -t

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

我为我的域 example.co 和 www.example.co 编写代码:/etc/nginx/sites-available/example.conf

server {
    listen 80;
    listen [::]:80;
    server_name example.co www.example.co;
    return 301 $scheme://www.example.co$request_uri;
}

server {
    listen 443 ssl http2 ;
    listen [::]:443 ssl http2 ;

    server_name example.co www.example.co;

    access_log /var/log/nginx/example-co.log;

    location / {
        proxy_pass "http://127.0.0.1:1000";
        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;
    }

    ssl_certificate /etc/letsencrypt/live/example.co/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.co/privkey.pem;
}

IP:127.0.0.1:1000 example.co 的 Node.js 项目 我的问题是 example.co 工作正常,但 www.example.co 不工作

我还为我的域 sub1.example.co 和 www.sub1.example.co 编写了代码:/etc/nginx/sites-available/sub1-example.conf

server {
    listen 80;
    listen [::]:80;
    server_name sub1.example.co www.sub1.example.co;
    return 301 $scheme://www.example.co$request_uri;
}

server {
    listen 443 ssl http2 ;
    listen [::]:443 ssl http2 ;

    server_name sub1.example.co www.sub1.example.co;

    access_log /var/log/nginx/sub1-example-co.log;

    location / {
        proxy_pass "http://127.0.0.1:2000";
        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;
    }

    ssl_certificate /etc/letsencrypt/live/example.co/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/example.co/privkey.pem; # managed by Certbot
}

IP:127.0.0.1:2000 sub1.example.co 的 Node.js 项目,

我的 sub1.example.co 无法正常工作

最佳答案

您需要使用 443www.example.co 添加到您的服务器 block :

server {
    .....
     server_name example.co www.example.co;
    .....
}

对于您的子域,建议使用另一个文件来配置相同格式的服务器 block :一个服务器 block 用于 80,另一个用于 443。:)

关于node.js - 我需要使用 nginx 服务器为 node.js 项目设置我的域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56702458/

相关文章:

ruby-on-rails - 如何使用子目录而不是子域?

javascript - 如何在 NodeJS 项目中应用 OO 设计模式?

node.js - React 构建和后端 Express.js

node.js - 从 node.js session 获取信息与从数据库获取信息

PHP 应用程序重定向周期

php - 如何在 Ubuntu 上安装这些 PHP 扩展?

nginx - 重用 nginx.conf 中域的配置语句

ruby-on-rails - 将多个子域指向同一个 heroku 应用程序

node.js - 将 Pug 与 NodeJS 和 Webpack 结合使用

security - 将内容安全策略框架祖先与 HAproxy 结合使用