Need help setting nginx up as proxy

I’m trying to setup nginx as a front-end receiving requests on port 80,
and then forwarding them to another nginx installation listening on a
different port.

The Nginx on port 80 is working okay, I opened up http://localhost/ and
got the ‘Welcome to Nginx’ message.
The Nginx on port 7776 is working okay, I opened up
http://static1.photosite.com:7776/js-min.js and got the page loaded
okay.

But when I try to access the same address by port 80
(Website Hosting - Mysite.com), instead of getting the page, I
get 502 Bad Gateway.

The conf for Nginx on Port 80 is:

worker_processes 1;

pid /var/run/nginx.pid;

events {
worker_connections 1024;
}

http {
include mime.types;
default_type application/octet-stream;

sendfile        on;

keepalive_timeout  65;

server {
    listen       80;
    server_name  localhost;

    location / {
        root   html;
        index  index.html index.htm;
    }

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }
}

server {
    listen 80;
    server_name www.photosite.com photosite.com 

static1.photosite.com static2.photosite.com;
location / {
proxy_pass http://127.0.0.1:7776;
}
}
}

The conf for the Nginx on port 7776 is:

worker_processes 2;

pid logs/nginx.pid;

events {
worker_connections 1024;
}

http {
include mime.types;
default_type application/octet-stream;

server_names_hash_bucket_size 128;

sendfile        on;
keepalive_timeout  10 10;

gzip  on;
gzip_comp_level 3;
gzip_proxied any;
gzip_types text/plain text/html text/css application/x-javascript 

text/xml application/xml application/xhtml+xml application/xml+rss
text/javascript;
gzip_disable “MSIE [1-6].”;

log_format main '$remote_addr - $remote_user [$time_local] '
             '"$request" $status $body_bytes_sent "$http_referer" '
             '"$http_user_agent"';

include sites/*;

}

and relevant part of the site file included:

server {
listen 7776;
server_name static1.photosite.com static2.photosite.com;

error_log  logs/photosite-static1-static2-error.log debug;
access_log  logs/photosite-static1-static2-access.log  main;

#Block access to dot files
location ^~ /. {
    deny all;
}

#Fix IP address
set_real_ip_from   127.0.0.1;
real_ip_header     X-Forwarded-For;

location / {
    root  /home/djeyewater/webapps/htdocs/photosite/CSI;
    expires max;
}

location /Img/desktop {
    add_header Content-Disposition: "$request_filename";
}

}

Can anyone help me get this working?

Thanks

Dave

Posted at Nginx Forum:

Fixed it now - I had copied the proxy syntax from the nginx.conf.default
example of proxying to apache, but it seems that the following lines are
also needed in the conf for Nginx on port 80, otherwise the Nginx
listening on the other port doesn’t receive the domain being accessed:

  proxy_set_header X-Real-IP $remote_addr;
  proxy_set_header Host $host;
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

Dave

Posted at Nginx Forum:

On Tue, Jan 12, 2010 at 4:17 AM, djeyewater [email protected]
wrote:

Fixed it now - I had copied the proxy syntax from the nginx.conf.default example of proxying to apache, but it seems that the following lines are also needed in the conf for Nginx on port 80, otherwise the Nginx listening on the other port doesn’t receive the domain being accessed:

                   proxy_set_header X-Real-IP $remote_addr;
                   proxy_set_header Host $host;
                   proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

Dave

Dave,

I think the general assumption is that not every backend cares about
the host header, because you may already be doing host header stuff on
the frontend and the backends might just be listening to a specific
port. This is why Host and other headers are not passed by default.

– Merlin