简体   繁体   中英

How to setup nginx proxy to another service on Jelastic

I want to setup prerender for my SPA. Prerender is node.js app which is also running on Jelastic. I'm using static file hosting to host my SPA.

I want to pass all requests from regular users to my site to static file hosting and pass all requests from searchbots to prerender.

To perform this, I'm trying to setup next config taken from prerender site:

user nginx;
worker_processes  auto;
worker_rlimit_nofile 2048;

error_log /var/log/nginx/error_log info;

events {
        worker_connections  2048;
        use epoll;
}

http {

  include             /etc/nginx/mime.types;
  default_type        application/octet-stream;

  upstream static-hosting {
    server my-site.hosting.com;
  }

  server {
    listen 80;

    location / {
      try_files $uri @prerender;
    }

    location @prerender {
      set $prerender 0;
      if ($http_user_agent ~* "googlebot|bingbot|googlebot-mobile|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;
      }

      if ($prerender = 1) {
        set $prerender "10.50.7.128:3000"; # this is my node.js node api
        rewrite .* /static/http://$host$request_uri? break;
          proxy_pass http://$prerender;
      }
      if ($prerender = 0) {
        proxy_pass http://static-hosting ;
      }
    }
  }

}

Not surprised, but this doesn't working, nor straight proxying, nor passing to prerender. Exactly this config works fine in my other configurations running on regular VPS'es.

My questions:

  1. What I'm doing wrong?

  2. How to tell nginx proxy to pass requests to any available of prerender node if I want to use autoscaling features?

Possibly the root cause is in headers. As you're working without public IP, you're using platform reverse proxy. You can find the examples how to work with headers at the /etc/nginx/nginx-jelastic.conf file. Also, you can enable public IP for nginx node.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM