2017-09-22 112 views
0

您好,我已在(站點已啓用)文件夾下面的nginx配置,我想爲我的angularjs應用程序添加一些prerender.io配置,但它似乎不起作用。使用nginx和ssl配置prerender.io

我認爲,在這裏我們需要配置預配器以良好的ssl。

server { 
    listen 443; 
listen [::]:443 ssl; 
server_name www.test.com test.com; 
server_tokens off; 
if ($host = 'test.com') { 
    return 301 https://www.test.com$request_uri; 
} 

root /home/test/public; 

ssl on; 
ssl_certificate /etc/ssl/certs/nginx-selfsigned.crt; 
ssl_certificate_key /etc/ssl/private/nginx-selfsigned.key; 
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; 
ssl_prefer_server_ciphers on; 
ssl_session_cache shared:SSL:30m; 
ssl_session_timeout 1d; 
ssl_session_tickets off; 
ssl_dhparam /etc/ssl/certs/dhparam.pem; 
ssl_ciphers 'ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA'; 

ssl_stapling on; 
ssl_stapling_verify on; 
resolver 8.8.8.8 8.8.4.4; 



location/{ 
      proxy_set_header X-Real-IP $remote_addr; 
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
      proxy_set_header X-Forwarded-Proto $scheme; 
      add_header Strict-Transport-Security "max-age=31536000; 
      includeSubDomains" always; 
      proxy_pass http://127.0.0.1:3000/; 
      proxy_http_version 1.1; 
      proxy_set_header Upgrade $http_upgrade; 
      proxy_set_header Connection "upgrade"; 
      proxy_set_header Host $host; 
      server_tokens off; 
} 


} 

    server { 
    listen 80; 
    listen [::]:80; 
    server_name www.test.com test.com; 
    return   301 https://www.test.com$request_uri; 
    } 
+0

你看過prerender的nginx配置嗎? https://gist.github.com/thoop/8165802 – mcranston18

+0

嗨mcranston18謝謝你的回覆,但實際上我試過但沒有成功!爲此我要求 –

+0

你是什麼意思不起作用?你需要解釋錯誤 –

回答

0

嗨,這是我做的配置。當我使用nginx -t進行測試時,沒有錯誤,但是服務器不運行。

server { 
    listen 443; 
listen [::]:443 ssl; 
server_name www.test.com test.com; 
server_tokens off; 
if ($host = 'test.com') { 
    return 301 https://www.test.com$request_uri; 
} 

ssl on; 
ssl_certificate /etc/ssl/certs/nginx-selfsigned.crt; 
ssl_certificate_key /etc/ssl/private/nginx-selfsigned.key; 
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; 
ssl_prefer_server_ciphers on; 
ssl_session_cache shared:SSL:30m; 
ssl_session_timeout 1d; 
ssl_session_tickets off; 
ssl_dhparam /etc/ssl/certs/dhparam.pem; 
ssl_ciphers 'ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA'; 

ssl_stapling on; 
ssl_stapling_verify on; 

root /home/test/test/public; 
index index.html.gz; 


location/{ 
    try_files $uri @prerender; 
} 

location /prerender { 
#proxy_set_header X-Prerender-Token wGvPrLPdSuDSpoxyOa46; 
proxy_set_header X-Real-IP $remote_addr; 
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
proxy_set_header X-Forwarded-Proto $scheme; 
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always; 
proxy_http_version 1.1; 
proxy_set_header Upgrade $http_upgrade; 
proxy_set_header Connection "upgrade"; 
proxy_set_header Host $host; 
server_tokens off; 


location @prerender { 
#proxy_set_header X-Prerender-Token key; 
proxy_set_header X-Real-IP $remote_addr; 
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
proxy_set_header X-Forwarded-Proto $scheme; 
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" 
    always; 
proxy_http_version 1.1; 
proxy_set_header Upgrade $http_upgrade; 
proxy_set_header Connection "upgrade"; 
proxy_set_header Host $host; 
server_tokens off; 

set $prerender 0; 
if ($http_user_agent ~* "baiduspider|twitterbot|facebookexternalhit|rogerbot|linkedinbot|embedly|quora link preview|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)") { 
    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 .* /https://$host$request_uri? break; 
    proxy_pass http://$prerender; 
} 
if ($prerender = 0) { 
    rewrite .* /index.html break; 
} 
    } 

} 

server { 
    listen 80; 
    listen [::]:80; 
    server_name www.test.com test.com; 
    return 301 https://www.test.com$request_uri; 
}