Nginx reverse proxy not passing requests

nginxproxypassreverse-proxy

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

Best Answer

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.