0

I have a task to make server with apache and nginx. I use CentOS 6.5 with one public IP on eth0 and two internal on eth1(192.168.1.2) and eth2 (192.168.1.3). Apache must run two diffrents sites with virtualhost on one IP (192.168.1.2). Nginx must run another web site on public ip without domain name. When I type in the browser www.test1.com or www.test2.com it should open one of the sites which run on apache and when i type the public ip it should open the site which run on nginx server.

EDIT1:

For now this is my last solution.

/etc/nginx/conf.d/default.conf

server {
    listen 91.xxx.xxx.xxx:8080; 
    server_name _; # catch all
    root /usr/share/nginx/html; 
}

server {
    listen 91.xxx.xxx.xxx:80;
    server_name www.test1.com; 
    root /non/existant/or/error/pages; 

    location / {
       if ($host = "test1.com") 
           {
        proxy_pass http://test1.com; 
           }
       if ($host = "test2.net") 
           {
       proxy_pass http://test2.net;
           }
    }
}

etc/httpd/conf/httpd.conf

Listen 192.168.1.2:80
NameVirtualHost 192.168.1.2:80

<VirtualHost 192.168.1.2:80 >
    ServerAdmin webmaster@test1.com
    DocumentRoot "/var/www/test1.com/public_html"
    ServerName www.test1.com
    ServerAlias test1.com
    ErrorLog "/var/www/test1.com/error.log"
</VirtualHost>

<VirtualHost 192.168.1.2:80 >
    ServerAdmin webmaster@test2.com/public_html"
    ServerName www.test2.com
    ServerAlias test2.com
    ErrorLog "/var/www/test2.com/error.log"
</VirtualHost>

And /etc/hosts

91.xxx.xxx.xxx  rangelov310

#Virtual Hosts
192.168.1.2 test1.com
192.168.1.2 test2.com

It's work, but if someway to make the nginx site work on port 80 will be great.

0

You config dosn't look to bad. But you will have an issue with your public IP. You cannot make both (apache & nginx) listen on your Public IP and port 80. Anyway, the domain www.test1.com normaly point to one IP (either the private OR the public).

1
  • The domaim www.test1.com must be visible in global internet (specificly with record in /etc/hosts). Maybe Someway I could try to make nginx listen http request for some host and if it have domain name to send on apache. This is my guess. I am not sure is it right. – ValeriRangelov Nov 7 '14 at 15:42
0

Here is a possible way

nginx.conf

server {
    listen xxxx:80; # public IP port 80 only
    server_name _; # catch all
    root /some/path/to/static; # static content
}

server {
    listen xxxx:80;
    server_name www.test1.com # so we can handle public requests
    root /non/existant/or/error/pages; # for safety

    location / {
        proxy_pass 192.168.1.2; # apache site1
    }
}

server {
    listen xxxx:80;
    server_name www.test2.com # so we can handle public requests
    root /non/existant/or/error/pages; # for safety

    location / {
        proxy_pass 192.168.1.3; # apache site2
    }
}

apache

Listen 192.168.1.2:80
Listen 192.168.1.3:80

<VirtualHost 192.168.1.2:80>
   ServerAdmin   .......
   DocumentRoot  .......
   ServerName    www.test1.com
   ServerAlies   test1.com
</VirtualHost>

<VirtualHost 192.168.1.3:80>
   ServerAdmin   .......
   DocumentRoot  .......
   ServerName    www.test2.com
   ServerAlies   test2.com
</VirtualHost>
  • Make sure you have no other Listen directives or you will run into problems.
  • sites can be hosted on only 1 internal address - here it is for readability

Explanation

  • nginx is used to serve everything
  • by default it serves the static content
  • if site1 or site2 is requested it proxies the request to the apache that is listening on the private ip address(es)
  • apache listens ONLY on the internal addresses (no shenanigans here)
  • probably you will need the so called split-view dns so sites be accessible from both the internet at large and the internal network.
1
  • Ok this is a good idea, but in nginx conf file has some wrong, because it open the first site which is in default.conf and the rest don't. Maybe it should make check some like that if(servername = test1.com) ... I don't know this is possible, but this is my idea. – ValeriRangelov Nov 7 '14 at 18:27
0

This is my solution and it work for me.

/etc/nginx/conf.d/default.conf

server {
    listen 91.xxx.xxx.xxx:8080;
    server_name rangelov310;
    root /var/www/some/directory;
    index  index.php;
        location ~ \.php$ {
        try_files $uri =404;
        allow all;
        fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
                }
      }


server {
    listen 91.xxx.xxx.xxx:80;
    server_name _;  
    location / {
       if ($host = "test1.com") {  #check what address is send from browser
        proxy_pass http://test1.com; }
       if ($host = "test2.net") {
       proxy_pass http://test2.net; }
    } 
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.