如何为自己的预渲染服务器配置Nginx conf

时间:2018-08-04 09:25:05

标签: nginx prerender

我想将nginx配置为使用通过pm2启动的预渲染服务器(localhost:3005) 我的网站位于本地主机上:3000

实际上,我正在使用service.prerender.io,但不适用于我的本地服务器。 它在本地工作,但不适用于nginx 我尝试了代理通行证:http://localhost:3005http://127.0.0.1:3005 但错误返回我:502错误的网关

帮助!

server {
    listen 80;
    listen [::]:80 default_server ipv6only=on;
    return 301 https://$host$request_uri;

}

server {
    listen 443;
    server_name www.hidden.com;

    ssl on;
    # Use certificate and key provided by Let's Encrypt:
    ssl_certificate /etc/letsencrypt/live/www.hidden.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/www.hidden.com/privkey.pem;
    ssl_session_timeout 5m;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;
    ssl_ciphers 'EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH';



location / {
    proxy_set_header X-Prerender-Token hiddentokenforstackoverflow;

    set $prerender 0;
    if ($http_user_agent ~* "googlebot|baiduspider|twitterbot|facebookexternalhit|rogerbot|linkedinbot|embedly|quora link preview|NinjaBot|showyoubot|outbrain|pinterest|slackbot|vkShare|W3C_Validator") {
        set $prerender 1;
    }
    if ($args ~ "_escaped_fragment_") {
        set $prerender 1;
    }
    if ($http_user_agent ~ "Prerender") {
        set $prerender 0;
    }
    if ($uri ~* "\.(js|css|xml|less|png|jpg|jpeg|gif|pdf|doc|txt|ico|rss|zip|mp3|rar|exe|wmv|doc|avi|ppt|mpg|mpeg|tif|wav|mov|psd|ai|xls|mp4|m4a|swf|dat|dmg|iso|flv|m4v|torrent|ttf|woff|svg|eot)") {
        set $prerender 0;
    }

    #resolve using Google's DNS server to force DNS resolution and prevent caching of IPs
    resolver 8.8.8.8;

    if ($prerender = 1) {
        #setting prerender as a variable forces DNS resolution since nginx caches IPs and doesnt play well with load balancing
        set $prerender "service.prerender.io";
        rewrite .* /$scheme://$host$request_uri? break;
        proxy_pass http://$prerender;
    }

    # Proxy_pass configuration
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_set_header X-NginX-Proxy true;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_max_temp_file_size 0;
    proxy_pass http://0.0.0.0:3000;
    proxy_redirect off;
    proxy_read_timeout 240s;
    }

   
   


    
    
}

你能告诉我如何配置我的conf文件吗

nginx返回此错误:

the error log return me this error:

2018/08/04 13:58:00 [error] 30272#30272: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 86.74.184.169, server: www.testeed.com, request: "GET /?_escaped_fragment_= HTTP/1.1", upstream: "http://127.0.0.1:3005/https://testeed.com/%3F_escaped_fragment_=", host: "testeed.com"
2018/08/04 13:58:00 [error] 30272#30272: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 86.74.184.169, server: www.testeed.com, request: "GET /?_escaped_fragment_= HTTP/1.1", upstream: "http://[::1]:3005/https://testeed.com/%3F_escaped_fragment_=", host: "testeed.com"

0 个答案:

没有答案