[HOWTO] Set up mod_proxy for virtual hosts

Post your questions about Web Server usage and Apache + PHP + MySQL/SQLite web applications.
User avatar
forkless
Experience counts
Posts: 1918
Joined: Mon Nov 23, 2009 6:52 am
Location: The Netherlands
Contact:

[HOWTO] Set up mod_proxy for virtual hosts

Postby forkless » Tue May 15, 2012 9:20 am

[Rationale]
You have a web app on another server that is running on the same ports as the primary web server (QNAP) to which the requests are being forwarded but you would like to access that application from the WAN side.


[Problem Description]
Port forwarding on a NAT enabled router will only allow to forward requests of the same port to a single IP address within your LAN. To overcome this problem this method can be used to let the QNAP webserver act as a proxy for another application.


[Caveat]
This howto does not handle the security of the target web application you will be proxying.


[Setting up virtual hosts manually]
While the QNAP offers virtual host support from the GUI, it lacks customization. Therefore I suggest using this fine HOWTO; viewtopic.php?f=32&t=14450


[Adding mod_proxy support]
Open a terminal session to your QNAP or use a tool like WinSCP to edit your /etc/config/apache/apache.conf file. Just before the custom vhost include you created before add the following line;


Code: Select all

/etc/config/apache/extra/httpd-proxy.conf


Save the /etc/config/apache/apache.conf file.

Now create the /etc/config/apache/extra/httpd-proxy.conf and add the following to it;

Code: Select all

LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_http_module modules/mod_proxy_http.so


(mod_proxy_http.so is included for SSL proxying support)

[Editing your virtual host (suffix method)]
Edit your customized.conf file you have created using the virtual host HOWTO

Add the proxy section and ProxyPass/ProxyPassReverse directives as shown below;

Code: Select all

<VirtualHost *:80>

    ServerName www.mydomain.com
    ServerAlias domain.com

        <Proxy *>
        Order deny,allow
        Allow from all
        </Proxy>

ProxyPass /myapp http://otherserver/myapp
ProxyPassReverse /myapp http://otherserver/myapp

        DocumentRoot "/share/Qweb/vhosts/mydomain.com/htdocs"

        <Directory "/share/Qweb/vhosts/mydomain.com/htdocs">
            Options FollowSymLinks Multiviews
            Order allow,deny
            Allow from all
        </Directory>

</VirtualHost>


Save the settings made to your custom virtual host include.

This method will create access to the app via http://www.mydomain.com/myapp


[Editing your virtual host (sub-domain method)]
Alternatively you can use this method if you prefer to access your app via a DNS sub-domain. I have optionally added ports on this example, while they are not needed they are there as an example to show port redirection. In this particular instance tcp/7000 will be redirected to the default tcp/80 for http.

Again, edit your custom.conf

Code: Select all

<VirtualHost *:80>

    ServerName myapp.mydomain.com
    ServerAlias myapp.mydomain.com

ProxyRequests Off
ProxyVia Off

        <Proxy *>
        Order deny,allow
        Allow from all
        </Proxy>

ProxyPass / http://otherserver:7000/
ProxyPassReverse / http://otherserver:7000/

</VirtualHost>


Save the settings made to your custom virtual host include.

This method will create access to the app via http://myapp.mydomain.com


[Restarting Apache]
To get things working you now need to either restart the Apache web server from the QNAP GUI or issue the following command from a terminal session;

Code: Select all

/etc/init.d/Qthttpd.sh restart



You are now done and should be able to access your app via the Apache proxy on your QNAP.

Please feel free to comment and/or make suggestions to improve.

Enjoy,
fork
Last edited by forkless on Fri Nov 30, 2012 7:55 pm, edited 6 times in total.

lmolenaar
Know my way around
Posts: 164
Joined: Mon May 19, 2008 4:34 pm
Location: Geldrop
Contact:

Re: [HOWTO] Set up mod_proxy for virtual hosts

Postby lmolenaar » Wed Jul 18, 2012 2:25 am

EDIT: updated post to add details
------------------------------

Hi forkless,

I would very much like to use a reverseproxy but I fail to find the "httpd-proxy.conf" file you mention. Could you help me get started?

Some info about my system (509pro):

[/etc/config/apache/extra] # ls

Code: Select all

apache-dav.conf                  httpd-autoindex.conf             httpd-manual.conf                httpd-ssl.conf
apache-msv2.conf                 httpd-dav.conf                   httpd-mpm.conf                   httpd-userdir.conf
apache-musicstation.conf         httpd-default.conf               httpd-multilang-errordoc.conf    httpd-vhosts-user.conf
apache-photo.conf                httpd-info.conf                  httpd-ssl-vhosts-user.conf       httpd-vhosts-user.conf.orig
apache-ssl.conf                  httpd-languages.conf             httpd-ssl-vhosts-user.conf.orig  httpd-vhosts.conf


[/usr/local/apache/modules] # ls

Code: Select all

eaccelerator.so*        mod_authn_alias.so*     mod_charset_lite.so*    mod_headers.so*         mod_proxy_balancer.so*  mod_ssl.so*
httpd.exp               mod_authnz_external.so* mod_disk_cache.so*      mod_ldap.so*            mod_proxy_connect.so*
imagick.so*             mod_authnz_ldap.so*     mod_expires.so*         mod_mem_cache.so*       mod_proxy_ftp.so*
libphp5.so*             mod_authz_unixgroup.so* mod_file_cache.so*      mod_proxy.so*           mod_proxy_http.so*
mod_auth_digest.so*     mod_cache.so*           mod_filter.so*          mod_proxy_ajp.so*       mod_proxy_scgi.so*


In order to use mod_proxy I needed to add the following to my config:

Code: Select all

LoadModule proxy_module modules/mod_proxy.so


The Vhost loads fine when using the IP name "http://testservername.eu" but when using the subdirectory "http://testservername.eu/movies" to trigger the proxypass I get:

Code: Select all

Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.

Please contact the server administrator, admin@NAS and inform them of the time the error occurred, and anything you might have done that may have caused the error.

More information about this error may be available in the server error log.


See below my vhosts.conf

Code: Select all

NameVirtualHost *:80


<VirtualHost _default_:80>
        DocumentRoot "/share/Qweb"
</VirtualHost>
<VirtualHost *:80>
   ServerName testservername.eu
   ServerAlias movie.testservername.eu
#BEGIN
        <Proxy *>
        Order deny,allow
        Allow from all
        </Proxy>

ProxyPass /movies http://127.0.0.1:5050/movie/
ProxyPassReverse /movies http://127.0.0.1:5050/movie/
#END

   DocumentRoot "/share/Qweb/site1"
   <Directory "/share/Qweb/site1">
      Options FollowSymLinks MultiViews
      Order allow,deny
      Allow from all
   </Directory>
</VirtualHost>
100Mb WAN <--> Asus RT-N56u <--> TS-509 Pro ultra (E7500, 4GB, 5x 3TB Seagate)
(Optware/Pure-ftpd/SSOT/SABNZBd/Sickbeard/Couchpotato)
What seems to be the officer problem?

User avatar
forkless
Experience counts
Posts: 1918
Joined: Mon Nov 23, 2009 6:52 am
Location: The Netherlands
Contact:

Re: [HOWTO] Set up mod_proxy for virtual hosts

Postby forkless » Fri Jul 20, 2012 6:46 am

I'veamended my HOWTO, the httpd-proxy.conf should include loadmodule you have added manually (my bad). You will also need to add mod_proxy_http.so for the reverse proxying portion if I'm not mistaken.

The omitted part ensures you are going into reverse proxy mode instead of forward (it shouldn't be necessary but I like to invoke it never the less).

Code: Select all

ProxyRequests Off
ProxyVia Off


Just add that before the <Proxy *> section.

Hope that helps resolving the error 500 you are getting right now :)
Supporting social media logins for Facebook, Google and Bitly https://qnap.community Image Image | Community App Repository

lmolenaar
Know my way around
Posts: 164
Joined: Mon May 19, 2008 4:34 pm
Location: Geldrop
Contact:

Re: [HOWTO] Set up mod_proxy for virtual hosts

Postby lmolenaar » Fri Jul 20, 2012 2:47 pm

All works now, thanks for your help!

The config I used:
[~] # vi /etc/config/apache/extra/httpd-vhosts-user.conf

Code: Select all

NameVirtualHost *:80

<VirtualHost _default_:80>
        DocumentRoot "/share/Qweb"
</VirtualHost>
<VirtualHost *:80>
        ServerName servername.com

        ProxyPreserveHost On
        ProxyRequests Off
        ProxyVia Off

        <Location /couchpotato>
                order deny,allow
                ProxyPass http://127.0.0.1:5050/couchpotato
                ProxyPassReverse http://127.0.0.1:5050/couchpotato
        </Location>

        <Location /sabnzbd>
                order deny,allow
                ProxyPass http://127.0.0.1:8800/sabnzbd
                ProxyPassReverse http://127.0.0.1:8800/sabnzbd
        </Location>

        <Location /sickbeard>
                order deny,allow
                ProxyPass http://127.0.0.1:7071/sickbeard/
                ProxyPassReverse http://127.0.0.1:7071/sickbeard/
        </Location>

        <Proxy *>
                Order deny,allow
                Allow from all
        </Proxy>

        DocumentRoot "/share/Qweb/site1"
        <Directory "/share/Qweb/site1">
                Options FollowSymLinks MultiViews
                Order allow,deny
                Allow from all
        </Directory>
</VirtualHost>
100Mb WAN <--> Asus RT-N56u <--> TS-509 Pro ultra (E7500, 4GB, 5x 3TB Seagate)
(Optware/Pure-ftpd/SSOT/SABNZBd/Sickbeard/Couchpotato)
What seems to be the officer problem?

lucaseva
New here
Posts: 2
Joined: Thu Sep 03, 2015 11:20 pm

Re: [HOWTO] Set up mod_proxy for virtual hosts

Postby lucaseva » Tue Sep 08, 2015 1:06 am

Hi to all,

I am trying to perform reverse proxy configuration. just in order to access to the virtualization station using http://IP/virtualization. and for docuwiki

Code: Select all

<VirtualHost *:80>
        ProxyPreserveHost On
        ProxyRequests Off
        ProxyVia Off

        <Location /docuwiki>
                order deny,allow
                ProxyPass https://127.0.0.1:8081/docuwiki
                ProxyPassReverse https://127.0.0.1:8081/docuwiki
        </Location>
        <Location /virtualization>
                order deny,allow
                ProxyPass https://127.0.0.1:8089/
                ProxyPassReverse https://127.0.0.1:8089/
        </Location>
        <Proxy *>
                Order deny,allow
                Allow from all
        </Proxy>
        DocumentRoot "/share/Web/docuwiki"
        <Directory "/share/Web/docuwiki">
                Options FollowSymLinks MultiViews
                Order allow,deny
                Allow from all
                CustomLog logs/main_log combined
                ErrorLog logs/error_log
                LogLevel debug
        </Directory>
</VirtualHost>


I have correclty modified my apache.conf but when I restart Apache the default config is loaded. so my config is overloaded.

Any idea ?

User avatar
forkless
Experience counts
Posts: 1918
Joined: Mon Nov 23, 2009 6:52 am
Location: The Netherlands
Contact:

Re: [HOWTO] Set up mod_proxy for virtual hosts

Postby forkless » Wed Sep 09, 2015 9:29 am

Unfortunately QNAP started to reset the Apache configuration after a restart in the newer firmwares. So this method does not work any longer unless you create some script to add the required lines (with sed for instance).
Supporting social media logins for Facebook, Google and Bitly https://qnap.community Image Image | Community App Repository

giopas
Been there, done that
Posts: 846
Joined: Thu Mar 26, 2015 12:36 am
Location: somewhere in EU

Re: [HOWTO] Set up mod_proxy for virtual hosts

Postby giopas » Mon Nov 16, 2015 4:54 pm

Hi forkless,

I am looking for a way to access to my KeyBox (web ssh terminal) which currently runs on port 58000 from behind my corporate firewall which only seems now to accept connections to 80 and 443 ports.

Is there a way to do that?

EDIT: yes, as described here: viewtopic.php?f=32&t=116833
Qnap TS-253Pro 16Gb RAM - Single Storage Pool: 2 WRed 4TB (RAID 1)
Qnap TS-453A 16Gb RAM - Single Storage Pool: 4 WRed 4TB (RAID 5)

123rolle
New here
Posts: 9
Joined: Wed Mar 24, 2010 5:26 am

Re: [HOWTO] Set up mod_proxy for virtual hosts

Postby 123rolle » Sun Mar 19, 2017 6:11 pm

I got everything working by using this:

Code: Select all

<VirtualHost *:80>
        ServerName blog.swanljung.fi
        ProxyPreserveHost On
        ProxyRequests Off
        ProxyVia Off
        <Location />
                Order deny,allow
                ProxyPass http://127.0.0.1:4000/
                ProxyPassReverse http://127.0.0.1:4000/
        </Location>
        <Proxy *>
                Order deny,allow
                Allow from all
        </Proxy>
        DocumentRoot "/share/Web/blog"
</VirtualHost>


Problem was virtual host turned itself off if I misconfigured virtualhost.

[/etc/config/apache] # /etc/init.d/Qthttpd.sh restart
Shutting down Qthttpd services: OK.
Recover apache confiugre <-------------- My confs were gone ! This happened on misconfiguration.
Starting Qthttpd services: OK


Return to “Web Server & Applications (Apache + PHP + MySQL / SQLite)”

Who is online

Users browsing this forum: No registered users and 4 guests