【问题标题】:i need to setup my domain with nginx server for node.js project我需要使用 nginx 服务器为 node.js 项目设置我的域
【发布时间】:2019-06-22 12:18:36
【问题描述】:

我已经在我的服务器中添加了 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 node.js 项目,例如 example.co 我的问题是 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 node.js 项目,用于 sub1.example.co,

我的 sub1.example.co 不工作

【问题讨论】:

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


    【解决方案1】:

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

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

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

    【讨论】:

    • 我添加了这个但无法正常工作 www.example.co 显示错误“无法访问此站点”
    • 在这种情况下,您可能错过了在 DNS 配置中添加 CNAME 记录,您将 A 记录映射到服务器 IP 地址。请确认您已添加此记录,然后重试。谢谢!
    • 我已经为 www 添加了 CNAME 并为我的 ip 和域添加了 A 记录
    • 我原来的答案仍然有效。您编辑的带有 443 的服务器块显示 sub1.example.co。您需要在 example.co 旁边添加 www.example.co 。否则,www.example.co 肯定无法访问。对于sub1.example.co,请使用/etc/nginx/sites-availabledefault文件旁边的另一个文件,不要忘记启用新的站点配置。
    • 我还更改了 server_name example.co www.example.co 并且我还为 sub1.example.co 创建了新的 conf 文件,但它不起作用
    猜你喜欢
    • 2020-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-23
    • 2010-09-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多