启用预渲染的 Nginx 上的反向代理 Nodejs

启用预渲染的 Nginx 上的反向代理 Nodejs

我想在 nginx 内部结合预渲染和快速 Web 服务器来运行 NodeJS。

到目前为止,我拥有的是一个稳定的反向代理配置,它运行良好 - 还有自定义的 502 页面和加密证书。

我想做的是将预渲染添加到这张图片中。我试过了配置,但它与我的反向代理冲突。

我当前的 nginx 配置运行良好:

server {
    server_name domain.com;
    location / {
        proxy_pass http://localhost:8000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-Proto http;
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_set_header X-Forwarded-Host $remote_addr;
        proxy_cache_bypass $http_upgrade;
        proxy_hide_header X-Powered-By;
        add_header Strict-Transport-Security "max-age=63072000; includeSubDomains" always;
        add_header X-Frame-Options "SAMEORIGIN" always;
        add_header X-XSS-Protection "1; mode=block" always;
        add_header X-Content-Type-Options "nosniff" always;
        add_header Referrer-Policy "origin-when-cross-origin" always;
    }
    error_page 502 /index.html;
    location /index.html {
        root /var/www/subdomains/maintenance;
    }

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/api.domain.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/api.domain.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}

server {
    if ($host = domain.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot

    listen 80;
    server_name domain.com;
    return 404; # managed by Certbot
}

答案1

经过一番努力,我终于找到了适合自己的配置。我使用了David Whitaker 发布的解决方案

使用 prerender.io 作为本地服务,NodeJS 网站运行良好。希望这对遇到同样问题的人有所帮助。

相关内容