0

I have a website setup http://mydomain.com/myroject on ubuntu 14.04(apache webserver)

I setup htsql service on the same host to run on port 5000

working links:
http://mydomain.com/myproject
http://mydomain.com:5000/region

Then I installed ssl cert on this server to run the website through https

https://mydomain.com/myproject is working but https://mydomain.com:5000/region is not working because the port 5000 is already in use where htsql service is running on this port

Now the question is how do I redirect on the same port(5000) from http to https with nginx reverse proxy

In other words https://mydomain.com:5000/region should work

My idea is to setup a different port(eg: 5001) on nginx and forward the request to https, 5000 port.

Here are apache configuration files:

/etc/apache2/ports.conf

Listen 80

<IfModule ssl_module>
    Listen 443
</IfModule>

<IfModule mod_gnutls.c>
    Listen 443
</IfModule>

/etc/apache2/sites-enabled/default.conf

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

<VirtualHost *:443>
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
        ServerName mydomain.com
        SSLEngine on
        SSLCertificateFile /home/ubuntu/project.crt
        SSLCertificateKeyFile /home/ubuntu/project.key
</VirtualHost>

nginx configuration file:

default.conf

server {
        listen 5001 ssl;

        server_name my domain.com;
        ssl on;
        ssl_certificate /home/ubuntu/project.crt;
        ssl_certificate_key /home/ubuntu/project.key;
        error_page 497 301 =307 https://mydomain.com:5001$request_uri;

        location / {
                proxy_pass https://mydomain:5000;
                proxy_redirect off;
                proxy_set_header Host $host:$server_port;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header X-Forwarded-Ssl on;
        }

}
  • 1
    If 5000/TCP is in use, you cannot use it for a 2nd purpose. – Rui F Ribeiro Feb 28 at 19:06
  • 1
    Apache is running on port 80 and 443 and serving http(s)://mydomain.com/myproject. The database is running on http://mydomain.com:5000/region. Correct me if I'm wrong. If you want an external connection to the database, you could use http(s)://mydomain.com/region (on port 80 and 443) and proxy the requests to http://mydomain.com:5000/region (internally) and close port 5000 in the firewall. No need for nginx. No need for an extra port 5001. See the mod_proxy Reverse Proxy example. – Freddy Feb 28 at 20:03
  • @Freddy Where should I change the configuration in order to use http(s)://mydomain.com/region (on port 80 and 443) and proxy the requests to mydomain.com:5000/region? – Nani Mar 1 at 21:28
1

In order to use Apache as a Reverse Proxy, make sure you have modules mod_proxy and mod_proxy_http enabled in your server (e.g. sudo a2enmod proxy_http). Add ProxyPass and ProxyPassReverse to the bottom of your VirtualHost sections and you should be good.

Restart the server afterwards with sudo service apache2 restart.

<VirtualHost *:80>
    # added missing ServerName
    ServerName mydomain.com
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    # should be disabled by default, just to make sure
    ProxyRequests Off

    ProxyPass /region  http://mydomain.com:5000/region
    ProxyPassReverse /region http://mydomain.com:5000/region
</VirtualHost>

<VirtualHost *:443>
        # moved ServerName to the top
        ServerName mydomain.com
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html

        # consider using separate log files for SSL
        #ErrorLog ${APACHE_LOG_DIR}/ssl-error.log
        #CustomLog ${APACHE_LOG_DIR}/ssl-access.log combined

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

        SSLEngine on
        SSLCertificateFile /home/ubuntu/project.crt
        SSLCertificateKeyFile /home/ubuntu/project.key

        # should be disabled by default, just to make sure
        ProxyRequests Off

        ProxyPass /region  http://mydomain.com:5000/region
        ProxyPassReverse /region http://mydomain.com:5000/region
</VirtualHost>

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.