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;
}
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.
No, this is not possible. Two applications cannot listen to the same port. You can solve this in two ways:
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
: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.
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:That's the same as
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:
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.