0

I have a Rails app (example1.com) host on a Nginx server using the following configuration :

server {
    access_log off;

    passenger_enabled on;
    client_max_body_size 5M;
    listen 10.10.10.10:80;
    server_name www.example1.com;
    rails_env production;

    root   /var/www/production/example1/public;

    location / {
        root   /var/www/production/example1/public;
        index  index.html index.htm;
    }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }
}

and everything works fine. However, now I have to install another app (example2.com) using Apache. My problem is that Apache and Nginx are actually on the same machine, using the same IP (public 238.x.x.x and private 10.x.x.x) and the same port (80). So everytime I'm trying to acces example2.com, it's actually showing me example1.com. Here is my Apache configuration:

<VirtualHost *:80>
    ServerAdmin [email protected]
    ServerName  www.example2.com

    DocumentRoot /var/www/production/example2
    DirectoryIndex index.html

    LogLevel Emerg
    ErrorLog /var/log/api/example2.com_err
    CustomLog /var/log/api/example2.com_cust Combined

    <Directory /var/www/production/example2>
        Options +ExecCGI
        AllowOverride all
    </Directory>
</VirtualHost>

I heard that using ProxyPass may solve that issue but I lack of knowledge in that field and I need some assistance. Is there a way to do it?

1 Answer 1

2

This is fairly trivial. Nginx listens on the IP/port for domains it's interested in, it passes requests to Apache that you tell it to. Just define another nginx server block, usually in a different configuration file to keep things easy for you - nginx doesn't care.

You may need some more statements in the proxy_pass area - try it with this simple case first and see how it goes.

server {
    # removed extra lines - put back as required
    listen 10.10.10.10:80; # This line likely unnecessary
    server_name www.example2.com;

    location / {
        proxy_pass 10.x.x.x:81
    }
}

Change Apache to listen on a different port. eg

<VirtualHost *:81>

Though you could do away with Apache entirely and have Nginx front both apps, unless you really need Apache for some reason. Something like this for the new location, it looks like you're just serving static files.

server {
    # removed extra lines - put back as required. Logging etc.
    listen 10.10.10.10:80; # This line likely unnecessary
    server_name www.example2.com;

    root /var/www/production/example2;
}
5
  • Thanks for this quick answer. When trying to save my nginx conf, it says Invalid url prefix for the line proxy_pass 10.x.x.x:81. Also, I'm using Apache because my app (example2) use a complex .htaccess file
    – El - Key
    Mar 19, 2016 at 2:01
  • Ok I needed to add http before the ip address (http://10.x.x.x:81). However, now I've got a 502 bad gateway error. Ideas? Here is the error I've got 2016/03/19 13:12:41 [error] 32662#0: *21 connect() failed (111: Connection refused) while connecting to upstream, client: 10.x.x.x, server: www.example2.com, request: "GET / HTTP/1.1", upstream: "http://10.x.x.x:81/", host: "www.example2.com"
    – El - Key
    Mar 19, 2016 at 2:13
  • Bad gateway means your haven't specified the URL of your Apache server correctly. Try curling it, copy that URL into the configuration. proxy_pass localhost:81 should work if Apache is listening on port 81, which you'd have to configure separately then restart apache.
    – Tim
    Mar 19, 2016 at 3:36
  • Thanks it worked. I actually didn't need tu use the port 81. By doing a proxy_pass http://localhost:80 it does work. Thanks
    – El - Key
    Mar 19, 2016 at 3:59
  • Great. That's a bit strange though, given Nginx listens on port 80 it'd be like sending the proxy to itself. It could be that they're listening slightly differently in a way that happens to work. Having Apache listen on port 81 would probably be a little more robust.
    – Tim
    Mar 19, 2016 at 5:19

You must log in to answer this question.

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