Nginx:添加上下文(如果不存在) - 在重定向中

时间:2015-05-09 03:49:29

标签: ruby-on-rails nginx

要求:

http://ruby.server.com重定向到https://ruby.server.com/app1/,如果传入的网址中包含上下文(例如:http://ruby.server.com/app2/),则不会附加任何内容

设置

我们有以下设置

亚马逊负载均衡器

| http -  |
          | --> 80(ruby.server.com)
| https - | 

nginx在端口80的ruby.server.com上运行.nginx中没有443。

 server {
     listen 80;
     server_name  ruby.server.com;
     root /home/ubuntu/ruby/server/public/;
     location / {
        if ($http_x_forwarded_proto != 'https') {
            rewrite ^ https://$host$request_uri? permanent;
        }
         client_max_body_size 100M;
         proxy_set_header X-Forwarded-Host $host;
         proxy_set_header X-Forwarded-Server $host;
         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
         proxy_pass http://127.0.0.1:4009/;
         root /home/ubuntu/ruby/server/public/;
   }
  

if($ http_x_forwarded_proto!='https')行用于将http://ruby.server.com重定向到https://ruby.server.com

更多背景

我们在rails服务器,/ public / app1,/ public / app2中运行了2个rails应用程序。默认情况下,我们要重定向ruby.server.com以转到app1。

我们可以在rails本身内解决上述问题,包括额外的重定向。我们试图看看我们是否可以在nginx层中附加该上下文。

解决方案基于@ timothy的说明。

server {
     listen 80;
     server_name  ruby.server.com;
     root /home/ubuntu/staging/server/public/;
     location / {
        if ($http_x_forwarded_proto != 'https') {
            rewrite ^ https://$host/app1/? permanent;
        }

         client_max_body_size 100M;
         proxy_set_header X-Forwarded-Host $host;
         proxy_set_header X-Forwarded-Server $host;
         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
         proxy_pass http://127.0.0.1:4009/;
         root /home/ubuntu/staging/server/public/;
         #auth_basic "Restricted";
         #auth_basic_user_file /etc/nginx/.htpasswd;
    }
    location ^~ /[^\/]+/ {
        if ($http_x_forwarded_proto != 'https') {
            rewrite ^ https://$host$request_uri? permanent;
        }

        client_max_body_size 100M;
        proxy_set_header X-Forwarded-Host $host;
        proxy_set_header X-Forwarded-Server $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass http://127.0.0.1:4009/;
    }
}

1 个答案:

答案 0 :(得分:1)

server {
    listen 80;
    server_name  ruby.server.com;
    root /home/ubuntu/ruby/server/public/;

    location / {
        # Default location for:
        #     http://ruby.server.com
        #     https://ruby.server.com

        if ($http_x_forwarded_proto != 'https') {
            rewrite ^ https://$host$request_uri? permanent;
        }
        client_max_body_size 100M;
        proxy_set_header X-Forwarded-Host $host;
        proxy_set_header X-Forwarded-Server $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass http://127.0.0.1:4009/;
    }
    location ^~ /[^\/]+/ {
        # Location for:
        #    http://ruby.server.com/anything/
        #    https://ruby.server.com/anything/

        # Do whatever you need here. :)

        client_max_body_size 100M;
        proxy_set_header X-Forwarded-Host $host;
        proxy_set_header X-Forwarded-Server $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass http://127.0.0.1:4009/;
    }
}