2
server {
    listen       81;
    server_name  example.com localhost direct1.example.com www.example.com admin.example.com forum.example.com;

    location /media/ {
        root /var/www/vhosts/example.com/html;
    }

    error_page  404              /404.html;

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    location / {
        proxy_set_header Host $host;
        proxy_set_header Connection Close;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        root /var/www/vhosts/example.com/html;
        proxy_pass   http://127.0.0.1:80;
    }
}

This is what I have in nginx/defaults.conf. The server is currently using Apache fine, and Plesk, which seems to manage vhosts on its own (somehow).

Any requests to example.com:81/media/image.jpg work fine, but example.com:81 uses an incorrect vhost.

I'm not 100% sure how the vhost situation works with Plesk, but it was installed and the default vhost is server1.example.com, and no matter how much fiddling with the proxy settings I do, I can't get it to use the example.com vhost.

edit added a ; to the root line under location. It was missing, I tried adding it but still no luck.

update added ServerName "example.com" to /etc/httpd/conf.d/zz010_psa_httpd.conf. Now under apache-status, it lists the vhost as example.com, but still "uses" server1.example.com. Boo.

It also seems that it's using DocumentRoot from the main apache httpd.conf, and not using the proper virtual host.

1 Answer 1

0

From the nginx docs on proxy_pass:

Note that the HTTP Host header is not forwarded, but is set based on the proxy_pass statement.

This means your proxy_set_header Host $host; line has no effect. The actual Host header passed to Apache will be Host: 127.0.0.1 as per the proxy_pass statement.

I recommend this location / section to proxy as you described:

location / {
    proxy_set_header Connection Close;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_pass http://example.com:80;
}
9
  • Ah, thanks. So far that works. Can't believe I missed that.
    – paimoe
    Dec 2, 2011 at 7:49
  • By default, nginx will use the proxy_pass argument for the Host header of the proxied request, but proxy_set_header Host $host; will override the default behavior and pass on the Host header from the original request. The fact that this change is fixing the problem tells me that the vhosts weren't all set up to be available on 127.0.0.1 in apache.
    – kolbyjack
    Dec 2, 2011 at 12:09
  • despite the docs specifically saying you can't forward Host?
    – lunixbochs
    Dec 2, 2011 at 13:39
  • Despite the docs saying that the default is to use the proxy_pass argument. You can override the default.
    – kolbyjack
    Dec 2, 2011 at 17:48
  • have you tested this personally? failed completely last time I tried to do it exactly how you said, as per the part of the docs saying you can't override the Host.
    – lunixbochs
    Dec 2, 2011 at 20:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .