用于枪角兽和 prerender.io 的NGINX配置



我目前正在使用Nginx和Gunicorn为我的网站提供服务。特别是,Nginx正在提供静态文件,Gunicorn正在提供rest-api。这是我当前的 Nginx 配置:

worker_processes 2;
user nobody nogroup;
# 'user nobody nobody;' for systems with 'nobody' as a group instead
error_log  /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
  worker_connections 1024; # increase if you have lots of clients
  accept_mutex on; # set to 'on' if nginx worker_processes > 1
  # 'use epoll;' to enable for Linux 2.6+
  # 'use kqueue;' to enable for FreeBSD, OSX
}
http {
  include mime.types;
  # fallback in case we can't determine a type
  default_type application/octet-stream;
  access_log /var/log/nginx/access.log combined;
  sendfile on;
  proxy_connect_timeout       300;
  proxy_send_timeout          300; 
  proxy_read_timeout          300;
  send_timeout                300;
  upstream app_server {
    # fail_timeout=0 means we always retry an upstream even if it failed
    # to return a good HTTP response
    # for UNIX domain socket setups
    # server unix:/tmp/gunicorn.sock fail_timeout=0;
    # for a TCP configuration
    server 127.0.0.1:8181 fail_timeout=0;
  }
  server {
    listen 80;
    listen [::]:80;
    server_name  www.miralytics.social;
    return 301 https://www.miralytics.social$request_uri;
}
  server {
    # if no Host match, close the connection to prevent host spoofing
    listen 443 default ssl;
    ssl_certificate      /certificates/fullchain1.pem;
    ssl_certificate_key  /certificates/privkey1.pem; 
    server_name www.miralytics.social;
    gzip on;
gzip_vary on;
gzip_types text/plain text/html text/xml text/css application/x-javascript image/png image/jpeg application/javascript application/octet-stream application/json;
gzip_proxied any;
gzip_http_version 1.1;
gzip_min_length 0;
gzip_comp_level 9;
gzip_buffers 16 8k;
    proxy_connect_timeout       600;
  proxy_send_timeout          600;
  proxy_read_timeout          600;
  send_timeout                600;
    keepalive_timeout 5;
    # path for static files
    root /home/edge7/UIBackend/dist;
    location ~ ^/(images|javascript|js|css|flash|media|static)/  {
      root    /home/edge7/UIBackend/dist;
      expires 1d;
    }
    location /auth/register {
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header X-Forwarded-Proto $scheme;
      proxy_set_header Host $http_host;
      # we don't want nginx trying to do something clever with
      # redirects, we set the Host: header above already.
      proxy_redirect off;
      proxy_pass http://localhost:8181;
    }
    location /auth/login {
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header X-Forwarded-Proto $scheme;
      proxy_set_header Host $http_host;
      # we don't want nginx trying to do something clever with
      # redirects, we set the Host: header above already.
      proxy_redirect off;
      proxy_pass http://localhost:8181;
    }
    location / {
      # checks for static file, if not found proxy to app
      try_files $uri @proxy_to_app;
    }

    location @proxy_to_app {
      proxy_pass http://localhost:8181;
    }
  add_header Cache-Control no-cache; #(no cache for testing reasons)

  }
}

这里是 Nginx 的官方预渲染配置,但正如你所看到的,它不适合我当前的配置,因为我已经有@proxy_to_app了。有人有过这方面的经验吗?

你可以稍微

修改一下你的配置,这样你就有这个了:

location / {
  # checks for static file, if not found proxy to app
  try_files $uri @proxy_to_app;
}

您可能希望将其更改为:

location / {
    proxy_set_header X-Prerender-Token YOUR_TOKEN;
    set $prerender 0;
    if ($http_user_agent ~* "googlebot|bingbot|yandex|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|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;
    }
  # checks for static file, if not found proxy to app
  try_files $uri @proxy_to_app;
}

最新更新