Welcome! Log In Create A New Profile

Advanced

Nginx proxying Apache + Multiple Domains

Posted by illarra 
Nginx proxying Apache + Multiple Domains
May 26, 2009 02:21PM
Hi everybody,

I want to have Nginx proxying to Apache, so that Apache is only worried about PHP, and all the static files are directly served by Nginx.
The idea is that all calls are managed by Nginx in the first moment.
So I have Nginx listening to my IP, and Apache listening to Localhost.
[b]With only one domain[/b], I can make it work: static for Nginx and PHP for Apache.

But, when I try to manage different domains, [url=http://forum.slicehost.com/comments.php?DiscussionID=1947]is always showing the last one in Apache config (or is it the first one? :)[/url]

To have an idea, this are some code snippets I'm using:
[code]### nginx/nginx.conf ###

proxy_redirect off;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

server {
listen MY.IP:80;
server_name domain1.com alias www.domain1.com;
location / {
proxy_pass http://127.0.0.1:80;
}
}

server {
listen MY.IP:80;
server_name domain2.com alias www.domain2.com;
location / {
proxy_pass http://127.0.0.1:80;
}
}[/code]
[code]### Apache conf. domain1 ###

<VirtualHost 127.0.0.1:80>
ServerName domain1.com
ServerAlias www. domain1.com
DocumentRoot /home/domain1/web/public
UserDir /home/domain1/homes/*/web/public
CustomLog /home/domain1/logs/access_log combined
<Directory /home/domain1/web/public>
Options -Indexes IncludesNOEXEC FollowSymLinks -MultiViews
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>[/code]
[code]### Apache conf. domain2 ###

<VirtualHost 127.0.0.1:80>
ServerName domain2.com
ServerAlias www. domain2.com
DocumentRoot /home/domain2/web/public
UserDir /home/domain2/homes/*/web/public
CustomLog /home/domain2/logs/access_log combined
<Directory /home/domain2/web/public>
Options -Indexes IncludesNOEXEC FollowSymLinks -MultiViews
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>[/code]
Can you give me any hint on how to configure Nginx and Apache to manage multiple domains?
Thanks.
Re: Nginx proxying Apache + Multiple Domains
May 26, 2009 10:20PM
It's possible that "Host" header is missing from client request? Try changing:

[code]
proxy_set_header Host $http_host;
[/code]

to:

[code]
proxy_set_header Host $host;
[/code]

That will set "Host" to the server name.

--
Jim Ohlstein
Re: Nginx proxying Apache + Multiple Domains
May 27, 2009 12:27PM
That change doesn't make any difference.
It always shows the first domain I configure in Apache.
Re: Nginx proxying Apache + Multiple Domains
May 27, 2009 12:35PM
Do you get any warnings when you run

# nginx -t

or when you start nginx?

--
Jim Ohlstein
Re: Nginx proxying Apache + Multiple Domains
May 27, 2009 12:46PM
It seems you are in the right path... :)
This is what I have
[code]
2009/05/27 16:45:21 [warn] 18277#0: conflicting server name "alias" on MY.IP:80, ignored
2009/05/27 16:45:21 [info] 18277#0: the configuration file /opt/local/etc/nginx/nginx.conf syntax is ok
2009/05/27 16:45:21 [emerg] 18277#0: open() "/var/spool/nginx/nginx.pid" failed (13: Permission denied)
2009/05/27 16:45:21 [emerg] 18277#0: the configuration file /opt/local/etc/nginx/nginx.conf test failed
[/code]



Edited 2 time(s). Last edit at 05/27/2009 12:53PM by illarra.
Re: Nginx proxying Apache + Multiple Domains
May 27, 2009 01:03PM
How were you reloading nginx between config changes? It should have choked on this the "[emerg]".

I though that "alias" didn't belong in server_name. You can safely remove that and leave as:

[code]
server {
server_name foo.bar www.foo.bar;
...
}
[/code]

Generally the nginx master runs as root. It then starts the workers which run as the user specified, or as the default ("nobody" unless specified otherwise in ./configure command IIRC).The master process does not serve any requests. It passes all requests to worker(s) so it's not a security risk.

If you are going to run the master process as another user, then that user will need to be able to write to the pid file and to the log files.

--
Jim Ohlstein



Edited 1 time(s). Last edit at 05/27/2009 01:05PM by Jim Ohlstein.
Re: Nginx proxying Apache + Multiple Domains
May 27, 2009 01:14PM
BTW, are you passing requests for *all* files to Apache? If you are, you are losing some of the great functionality of nginx which is its far superior ability to rapidly serve static files.

Another thing you can do to troubleshoot this, if it's still not working after fixing your nginx.conf, is to have Apache listen on 127.0.0.1 and 127.0.0.2 or, on 127.0.0.1:81 and 127.0.0.1:82 and use different IP or port for different virtual hosts.

You can also try putting your proxy parameters in a proxy.conf file and including it within each "server" block.

--
Jim Ohlstein
Re: Nginx proxying Apache + Multiple Domains
May 27, 2009 01:30PM
General comments:
[list]
[*] This is a simplified example, I'm using nginx to serve static contents from a specified folder (that works)
[*] This is what I have in the firts line "user nginx root"; the master process is owned by "root" and the rest are owned by "nginx". it seems there are no problems launching processes...
[*] You were right with the "alias". Now that I have deleted it, I don't get more errors
[/list]

I made it work pointing to different ports, like :80 for domain1.com, :8080 for sub.domain1.com, and :8088 for domain2.com...
But I wanted to know if this is a [b]clean solution[/b] or not...

By the way: thanks for the fast answers, Jim.



Edited 1 time(s). Last edit at 05/27/2009 01:34PM by illarra.
Re: Nginx proxying Apache + Multiple Domains
May 27, 2009 01:57PM
I'm not sure what is a "clean solution" but surely there are ways to make it work using a shared internal IP:port combination.

I run multiple virtual hosts on many boxes using php as a fastcgi and they all pass to 127.0.0.1:9000 without difficulty. Of course that uses the fastcgi module which is a bit different. But one thing I do is to include the fastcgi parameters in each server block (as "include /path/to/fastcgi_params;").

I don't use Apache much. I have used it for mod_perl and had no trouble using the same internal IP:port but did have some issues configuring SSL on multiple domains. I am no longer using mod_perl for that app but rather fcgiwrap and I find that much easier to use and configure.

I still wonder if including the proxy directives within each server block might not help you if you really have a strong desire to have all on 127.0.0.1:80. However, from a performance perspective I doubt you will see much difference if any. Consider that Apache commonly listens on multiple IP's and often on more than one port for each if SSL is in use.

--
Jim Ohlstein
Re: Nginx proxying Apache + Multiple Domains
May 29, 2009 11:13AM
I solved it using diferent ports: 80, 8000, 8010, 8011, etc.
Thanks for the help.
Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 223
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready