0

I am trying to masking a remote URL with Nginx using proxy_pass

I'd like to load staging.saas.localhost/_ when the browser url is saas.localhost/uk_staging.

For some reason the location in saas.localhost is not working, and for not working I mean that the location seems to be ignored.

saas.localhost/uk_staging is handled by the application and not from staging.saas.localhost/_, it seems to me that even for saas.localhost/uk_staging the location used is location ~ .php$

I created a 2nd domain t.saas.localhost and it is working as expected

The t.saas.localhost domain is working perfectly fine.

t.saas.localhost/uk_staging is displaying staging.saas.localhost/_ t.saas.localhost/anything_else is displaying google.co.uk/

This is my current Nginx conf:

server {
    listen 80;
    server_name   saas.localhost www.saas.localhost staging.saas.localhost;
    root /codebase/saas;
    index index.php index.html index.htm;

    location /uk_staging {
            #proxy_set_header Host $host;
            #proxy_set_header X-Real-IP $remote_addr;
            proxy_pass http://staging.saas.localhost/_;
    }

    if (!-e $request_filename) {
            rewrite ^(.*)$ /index.php?action=$1 last;
    }

    location ~ \.php$ {
            try_files $uri =404;
            fastcgi_pass unix:/tmp/php5-fpm.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
            fastcgi_read_timeout 600;
    }
}

server {
    listen 80;
    server_name  t.saas.localhost;
    root /codebase/saas;
    index index.php index.html index.htm;
    location /uk_staging {
            #proxy_set_header Host $host;
            #proxy_set_header X-Real-IP $remote_addr;
            proxy_pass http://staging.saas.localhost/_;
    }
    location / {
            #proxy_set_header Host $host;
            #proxy_set_header X-Real-IP $remote_addr;
            proxy_pass http://google.co.uk;
    }
}
Jeff
  • 5
  • 3
  • 1
    Maybe because you do a proxy pass to you own servername ? – Skamasle Oct 07 '14 at 12:08
  • Mhn, I don't think because if it were the case it should not work the t.saas.localhost as well. I configured all the host files Nginx seems ignoring the location in the 'location: /uk_staging' of the first configuration – Jeff Oct 07 '14 at 14:41

1 Answers1

0

Why are you redirecting to the same vhost with a proxy_pass directive ?!

Also, nginx elects the matching location a way you probably don't expect. Read this : Nginx rewite rules 403 error.

It's better not to use if when it's possible to avoid it.

server {
    listen 80;
    server_name   saas.localhost www.saas.localhost staging.saas.localhost;
    root /codebase/saas;
    index index.php index.html index.htm;

    location ^~ /uk_staging {
            #proxy_set_header Host $host;
            #proxy_set_header X-Real-IP $remote_addr;
            proxy_pass http://staging.saas.localhost/_; # What are you doing here ??!!
    }

    #avoid rewrite for static content
    location ~* \.(js|jpg|png|css|htm|html|gif|txt|swf|mpg|mp4|avi)$ {
            expires 30d;
    }


    location / {
        rewrite ^(.*)$ /index.php?action=$1 last;
    }

    location ~ \.php$ {
            try_files $uri =404;
            fastcgi_pass unix:/tmp/php5-fpm.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
            fastcgi_read_timeout 600;
    }
}
Xavier Lucas
  • 12,815
  • 2
  • 44
  • 50
  • @ proxy_pass http://staging.saas.localhost/_; # What are you doing here ??!! I trying to load the page: http://staging.saas.localhost/_, it's an actual page – Jeff Oct 07 '14 at 14:42
  • "Why are you redirecting to the same vhost with a proxy_pass directive ?!" Because 'staging.saas.localhost' is the staging environment whilst 'saas.localhost' is the live one. Everything is handled by the app, but I'd like to mask http://staging.saas.localhost/_ with http://saas.localhost/uk_staging – Jeff Oct 07 '14 at 14:48
  • 1
    @Jeff Do you realize they are served by the same server block ? I really don't follow your logic. Also, you are adding duplicate content in your production environment in order to serve a staging one ?? Maybe you need to post the whole configuration to make it clear because for what I see atm it's seems a huge mess for nothing. – Xavier Lucas Oct 07 '14 at 14:59
  • I think that make a bucle, for the same reaseon the second option (t.saas.localhost) work well for you – Skamasle Oct 07 '14 at 15:25
  • Your suggestion actually worked with a little tweak: location ~* \.(js|jpg|png|css|htm|html|gif|txt|swf|mpg|mp4|avi)$ { expires 30d; } to not rewrite static resources with location / { rewrite ^(.*)$ /index.php?action=$1 last; } – Jeff Oct 07 '14 at 15:35