gpt4 book ai didi

nginx - 无法让 prerender.io nginx 配置与 https 重写一起使用

转载 作者:行者123 更新时间:2023-12-05 00:57:20 25 4
gpt4 key购买 nike

我想我可能已经咬掉了我正在尝试编写的这个 nginx 配置。我是 nginx 配置的新手,我正在尝试编写一个非常复杂的配置(至少对我而言)。任何帮助将不胜感激。

配置需要:

  1. redirect http $http_x_forwarded_proto to https (https cuts off at the ec2 load balancer)
  2. work with prerender.io's nginx middleware (seo for crawlable single page application)
  3. pass any query parameters (_escaped_fragment_=) from http to https
  4. redirect all subdomain requests to https://example.com/c/$subdomain
  5. allow http://example.com/healthcheck.txt to pass through http (load balancer health check)


这是我当前的配置
server {
listen 80;
server_name example.com;

root /var/www/html/dist;
index index.html;

error_log /var/log/mysite/error.log;
access_log /var/log/mysite/access.log;

location /healthcheck.txt {
break;
}

location / {
try_files $uri @prerender;

if ($http_x_forwarded_proto != "https") {
set $urltest N;
}

if ($query_string) {
set $urltest "${urltest}Y";
}

if ($urltest = N) {
rewrite ^(.*)$ https://example.com$1 permanent;
}

if ($urltest = NY) {
rewrite ^(.*)$ https://example.com$1?$query_string permanent;
}
}

location @prerender {
proxy_set_header X-Prerender-Token MY_TOKEN;

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 .* /$scheme://$host$request_uri? break;
proxy_pass http://$prerender;
}
if ($prerender = 0) {
rewrite .* /index.html break;
}
}
}

server {
listen 80;
server_name ~^(?<sub>.+)\.example\.com$ ;

rewrite ^ https://example.com/c/$sub;
}

这适用于项目 1 4 和 5,但是项目 2 和 3 不起作用

  1. work with prerender.io's nginx middleware


这根本不起作用,但是它可以在没有 https 重定向的情况下工作

  1. pass any query parameters (_escaped_fragment_=) from http to https


它可以工作,但会像这样复制查询参数: ?_escaped_fragment_=&_escaped_fragment_=
任何人都可以提供任何建议?

最佳答案

我终于能够解决它了。我让 SSL 通过负载平衡器并到达服务器本身,这样我就不能简单地将 http 重定向到 https 而不必担心负载平衡器造成的复杂性。

这是我更新的配置:

server {
listen 80;
server_name ~^(?<sub>.+)\.example\.com$ ;

if ($sub = 'www') {
return 301 https://$host$request_uri;
}

if ($sub != '') {
rewrite ^ https://example.com/c/$sub;
}

if ($sub = '') {
return 301 https://$host$request_uri;
}
}

server {
listen 443;
server_name example.com;

ssl on;
ssl_certificate /etc/ssl/star_example_com.pem;
ssl_certificate_key /etc/ssl/star_example_com.key;

root /var/www/html/dist;
index index.html;

access_log /var/log/example/ssl.access.log;
error_log /var/log/example/ssl.error.log;

include /etc/nginx/content_redirects.conf;

location / {
try_files $uri @prerender;
}

location @prerender {
proxy_set_header X-Prerender-Token MY_KEY;

set $prerender 0;
if ($http_user_agent ~* "baiduspider|twitterbot|facebookexternalhit|rogerbot|linkedinbot|embedly|quora link preview|showyoubot|outbrain|pinterest|slackbot|vkShare|W3C_Valid
ator") {
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;
}
}
}

关于nginx - 无法让 prerender.io nginx 配置与 https 重写一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34776580/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com