2

I have ubuntu 16.04 with nginx and apache2. There are 2 DNS A records pointed to this machine:

  • app1.mydomain.com
  • app2.mydomain.com

I need app1.mydomain.com to be resolved by apache and app2.mydomain.com to be resolved by nginx. Both on port 80. Is it possible?

moreover apache needs to handle 2 different applications (site1 and site2).

I would like:

  • app1.mydomain.com/site1 - to be resolved by apache and run application site1
  • app1.mydomain.com/site2 - to be resolved by apache and fire application site2
  • app2.mydomain.com - to serve ghost application with nginx

This is my Apache configuration:

<VirtualHost *:80>
    ServerAdmin [email protected]

    ServerName app1.mydomain.com

    ServerAlias www.app1.mydomain.com

    ErrorLog /var/www/site1/logs/error.log

    CustomLog /var/www/site1/logs/access.log combined

    WSGIScriptAlias /api /var/www/site1/application/index.py/

    Alias /static /var/www/site1/application/static

    <Directory /var/www/site1/application>
            Order deny,allow
            Allow from all
    </Directory>

    AddType text/html .py

    ErrorLog /var/www/site2/logs/error.log
    CustomLog /var/www/site2/logs/access.log combined

    WSGIScriptAlias /site2 /var/www/site2/index.py/
    Alias /site2/uploads /var/sftp/site2/uploads/

    <Directory /var/www/site2/>
            Order deny,allow
            Allow from all
    </Directory>

This is my NGINX config file:

server {
listen 8080;
listen [::]:8080;

server_name app2.mydomain.com;
root /var/www/ghost/system/nginx-root;

location / {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header Host $http_host;
    proxy_pass http://127.0.0.1:2368;

}
location ~ /.well-known {
    allow all;
}

client_max_body_size 50m;
}
3

2 Answers 2

1

First of all. Apache and Nginx are Web servers - not dns servers. They do not resolve anything. This question has nothing to do with DNS, but everything with how web servers handle requests.

I need app1.mydomain.com to be resolved by apache and app2.mydomain.com to be resolved by nginx. Both on port 80. Is it possible?

No, this is not possible. Two applications cannot listen to the same port. You can solve this in two ways:

  1. Have Apache listen on port 80, and proxy requests for app2.mydomain.com to nginx, listening on a different port, and serving app1.mydomain.com straight.
  2. Opposite. Have nginx proxy for Apache.

Or - make both hosted by the same webserver. Apache and nginx are in many instances interchangeable on the technical level, so from the applications perspective it should not really matter. Management-wise they're rather different.

I note that you run nginx on port 8080, so I assume you want to use apache as a proxy. Then create a new Virtual Host for apache, e.g. /etc/apache2-sites-available/app2.mydomain.com.conf:

<VirtualHost *:80>
   DocumentRoot "/var/www"
   ErrorLog "logs/app2-error_log"
   CustomLog "logs/app2-access_log" common
   ServerName app2.mydomain.com
   ProxyRequests Off
   <Proxy *>
          Order deny,allow
          Allow from all
    </Proxy>
     Proxypass / http://localhost:8080/
</VirtualHost>

Then run sudo a2enmod proxy_http; sudo a2ensite app2.mydomain.com; sudo service apache2 reload.

This will enable mod_proxy, mod_proxy_http and make apache forward any requests for the VirtualHost app2.mydomain.com to nginx, which according to your config is running on port 8080.

I've not tested this config, so some tweaking may be needed.

As a sidenote: why do you need nginx? According to the nginx setup it just proxies a request for some other webserver running on port 2368. You can proxy directly using apache...

mod_proxy-documentation may be handy in tweaking it.

3
  • From the user's message, it doesn't appear he's confusing the web server with his DNS server. It appears he's saying his DNS servers are already configured and pointing to the machine's IP. Apr 8, 2018 at 16:33
  • Yes, and this is what I expect in my answer. It also looks like he's using nginx to proxy some other application...
    – vidarlo
    Apr 8, 2018 at 16:38
  • Thank you. It was what I was looking for. I am still experiencing some problems. I set nginx proxy to apache and it does work with proxy_pass app1.mydomain.com:80; but it does not work with proxy_pass 127.0.0.1:80; - something in apache should be set up I guess... Apr 9, 2018 at 21:13
0

By the content of your question, you already have it worked out, except that you can only access one application per port #. You can't run both Apache2 and Nginx on the same port. It's not possible.

Since you've mentioned you already have your DNS working and pointing to the correct machine (IP). You can reach your app1.mydomain.com by the default (port 80) with:

http://app1.mydomain.com

That's the same as

http://app1.mydomain.com:80

You will have to specify the port for your Nginx, which you have, by your configuration file, set for port 8080.

Use this to access your site1:

http://app1.mydomain.com:8080

If you specify the wrong port, the default page will load regardless of the domainname used. For Apache, the default is the first virtual host, unless specified different.

2
  • But the goal is to connect app1.mydomain.com with Apache and app2.mydomain.com with Nginx. Both with port 80 so that user is not forced to specify port. Apr 8, 2018 at 16:26
  • In that case you can have one server refresh and reload to the alternate server. Apr 8, 2018 at 16:30

You must log in to answer this question.

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