【问题标题】:How would you enable HTTPS on a default nextjs app, Linux您将如何在默认的 nextjs 应用程序 Linux 上启用 HTTPS
【发布时间】:2021-07-18 08:58:35
【问题描述】:

当我在我的 Linux 服务器上运行“next start”时,nextjs 似乎只托管在 HTTP 上。我已经安装了让我们加密,但似乎找不到将证书与默认的下一个 js 服务器链接的方法。但是,我看到有一个解决方案涉及创建一个 server.js 文件来手动启动下一个服务器,但使用它也会抑制 nextjs 使用服务器端渲染和无服务器功能等功能的能力。由于nextjs的主流使用,我很难相信没有办法解决这个问题?

如果有人找到解决此问题的方法或有任何信息,请分享。

【问题讨论】:

    标签: reactjs http https next.js lets-encrypt


    【解决方案1】:

    您需要设置 nginx 以将您的域路由到 Nextjs 端口。检查你的 nextjs 正在运行哪个端口。示例端口 3000 是默认端口。

    在您的服务器上,照常安装 nginx。网上很多教程。安装后:

    cd /etc/nginx/sites-available

    nano example.com

    从下面复制/粘贴 nginx 设置

    # *q is our domain, replace port 3000 with your port number
    server {
      listen 80;
      listen [::]:80;
    
      root /var/www/html;
      index index.html index.htm index.nginx-debian.html;
    
      server_name example.com www.example.com;
    
    location / {
            proxy_pass http://localhost:3000;
            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;
    }
      
      # for letsencrypt
      location ~ /.well-known {
        allow all;
      }
    }
    

    sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/example.com

    nginx -t

    服务 nginx 重启

    确保您的 Nextjs 正在运行。

    正常安装 Letsencrypt。网上有很多教程。

    sudo apt update && sudo apt install certbot python3-certbot-nginx

    certbot --nginx -d example.com -d www.example.com

    certbot certonly --standalone --preferred-challenges http -d example.com

    certbot renew --dry-run

    【讨论】:

    • 这似乎不太对劲。 OP没有要求使用https的方法吗?为什么要使用 80 端口?
    • 它需要为 http 监听端口 80,因此letsencrypt 可以检查站点是否存在以将其放入 https。
    • 这是一种解决方法,并且有效,但需要外部应用程序,nextjs 甚至应该提供 https 作为配置选项。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-19
    • 2021-07-29
    • 1970-01-01
    • 2014-12-12
    • 2023-04-04
    • 1970-01-01
    相关资源
    最近更新 更多