Nginx

啟用預渲染的 nginx 上的反向代理 nodejs

  • November 16, 2018

我想在 nginx 內部結合 prerender 執行帶有 express 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
}

經過一番掙扎,我設法找到了適合我的配置。我使用了David Whitaker 發布的解決方案

使用 prerender.io 作為本地服務,沿著 NodeJS 網站工作就像一個魅力。希望這對任何有同樣問題的人有所幫助。

引用自:https://serverfault.com/questions/939765