5

I have a domain (example.com) and a subdomain (sub.example.com) both hosted on the same server. I want to proxy a request so any file requested on http://sub.example.com/api/ will be requested on http://example.com/api/ (along with the arguments) Here is my nginx code from the sub.example.com:

location /api {
    rewrite /api/(.*) /api/$1 break;
    proxy_pass http://example.com/api;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_buffering off;
}

This does not work for me. What works is:

location ~* ^/api/(.*)$ {
    rewrite ^/api/(.*)$ http://example.com/api/$1 last;
}

EDIT: i forgot to say that my server definition contains some more rules regarding Yii framework and parsing of php files. Here is the complete server statement:

server {
        listen 80;
        server_name sub.example.com;
        access_log /var/log/nginx/sub.example.com.access_log main;
        error_log /var/log/nginx/sub.example.com.error_log info;
        root /var/www/localhost/htdocs/sub.example.com;
        index index.php index.html index.htm default.html default.htm;

#       location ~* ^/api/(.*)$ {
#                       rewrite ^/api/(.*)$ http://example.com/api/$1 last;
#               }

        location /api {
                        rewrite /api/(.*) /api/$1 break;
                        proxy_pass http://example.com/api;
                        proxy_set_header X-Real-IP $remote_addr;
                        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#                        proxy_set_header Host $http_host;
                        proxy_redirect off;
                        proxy_buffering off;
                        break;
        }

        location / {
                if (-f $request_filename) {
                        #expires max;
                        break;
                }
                if (!-e $request_filename) {
                        rewrite ^/(.*)$ /index.php?/$1 last;
                }
        }
        location /index.php {
                include /etc/nginx/fastcgi.conf;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME /path-to/index.php;
                fastcgi_param  REQUEST_URI      $request_uri;
                fastcgi_param  QUERY_STRING     $query_string;
                fastcgi_param  REQUEST_METHOD   $request_method;
                fastcgi_param  CONTENT_TYPE     $content_type;
                fastcgi_param  CONTENT_LENGTH   $content_length;
                fastcgi_pass 127.0.0.1:9000;
        }


        location ~ \.php$ {
                        fastcgi_pass  127.0.0.1:9000;
                        include /etc/nginx/fastcgi.conf;
                        fastcgi_index index.php;
                        fastcgi_intercept_errors on; # for easier debug
                }
}

1 Answers1

1

You are passing the initial request Host header sub.example.com to example.com :

proxy_set_header Host $http_host;

When proxying the request, it's likely falling in sub.example.com vhost or default vhost, not example.com vhost, depending on your setup.

Remove this line.

Xavier Lucas
  • 12,815
  • 2
  • 44
  • 50
  • I removed the line you said and it seems to be working for some of the files but for others not. I forgot to say that in sub.example.com there are some more rules Yii and php parsing. – pls see the update of the main post – Andrei Nastasa Oct 01 '14 at 09:48
  • @AndreiNastasa Can't help you without factual behaviour explained. Post examples with access log of `example.com` and initial requests on `sub.domain.com`. Also, post `example.com` configuration. – Xavier Lucas Oct 01 '14 at 11:22
  • Proxy_pass works fine without the php part: location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; include /etc/nginx/fastcgi.conf; fastcgi_index index.php; fastcgi_intercept_errors on; # for easier debug } If i issue a request like sub.example.com/api/test.php the server does not forward the request via proxy_pass, but tries to parse it with the php parser. Do you have any ideea how can i bypass the php parser for the proxy_pass folder? – Andrei Nastasa Oct 01 '14 at 13:12
  • @AndreiNastasa Use the `^~` operator in the prefixed location /api so it doesn't try to find a matching regex location i.e. `location ^~ /api { [...] }` – Xavier Lucas Oct 01 '14 at 13:21
  • nginx fails if i put a rex exp in location when using proxy_pass – Andrei Nastasa Oct 01 '14 at 13:47
  • @AndreiNastasa What do you mean? Please provide factual elements each time. This is not the regex operator `~` but the special prefix operator `^~`. Btw, remove unecessary `break;` calls. – Xavier Lucas Oct 01 '14 at 13:51