Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/git/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
gunicorn和prerender.io的NGINX配置_Nginx_Gunicorn_Prerender - Fatal编程技术网

gunicorn和prerender.io的NGINX配置

gunicorn和prerender.io的NGINX配置,nginx,gunicorn,prerender,Nginx,Gunicorn,Prerender,我目前正在使用Nginx和Gunicorn为我的网站提供服务。 特别是,Nginx服务于静态文件,Gunicorn服务于restapi。 这是我当前的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和Gunicorn为我的网站提供服务。 特别是,Nginx服务于静态文件,Gunicorn服务于restapi。 这是我当前的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_应用程序。
有人对此有经验吗?

您可以稍微修改一下配置,这样您就可以:

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;
}

您可以稍微修改一下配置,这样您就可以:

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;
}

嗨,Prerender(确实如此!),它工作得很好。我现在有一个不同的问题。我更新了我的try_文件如下:try_文件$uri$uri//index.html@proxy_to_app;html对于允许用户刷新页面(角度路由)很重要,问题是除了我的初始页面之外,我不能在您的页面上提交其他页面。我希望这个问题是有道理的,因为prerender配置已经在上面了,所以你不需要将@proxy_更改为_应用程序。你能给我们发一封电子邮件吗support@prerender.io在您的配置文件中提供更多信息,以便我们能够帮助诊断该问题?我将接受答案,因为它解决了我的原始问题。嗨,Prerender(确实如此!),这很好。我现在有一个不同的问题。我更新了我的try_文件如下:try_文件$uri$uri//index.html@proxy_to_app;html对于允许用户刷新页面(角度路由)很重要,问题是除了我的初始页面之外,我不能在您的页面上提交其他页面。我希望这个问题是有道理的,因为prerender配置已经在上面了,所以你不需要将@proxy_更改为_应用程序。你能给我们发一封电子邮件吗support@prerender.io在您的配置文件中提供更多信息,以便我们能够帮助诊断该问题?我将接受答案,因为它解决了我原来的问题。