3

I've got a raspberry pi working and being accessible through a domain, which is asada.net. It serves web pages using apache 2.2.22.

Besides, I recently bought a NAS which has a web page in a local apache instance (embedded), too.

Raspberry pi's local ip is 192.168.0.4 and NAS's local ip is 192.168.0.5.

Every time I access www.asada.net, raspberry pi's root page is shown.

Now, what I want to do is to redirect www.asada.net/nas to the NAS's ip, 192.168.0.5, so that this page gets accessible from outside.

I don't want asada.net to point to 192.168.0.5 nor making any changes to its apache server, just redirect it from raspberry pi.

So every configuration I've tried has been made in raspberry pi's apache. I've enabled mod_proxy; tried ProxyPass and ProxyPassReverse, I've also tried Location directive... But with no luck.

Could anybody help me out?

Thanks in advance!

EDIT: My raspberry pi is running Raspbian and apache's version is 2.2.22. These are loaded modules:

Loaded Modules:
 core_module (static)
 log_config_module (static)
 logio_module (static)
 version_module (static)
 mpm_prefork_module (static)
 http_module (static)
 so_module (static)
 alias_module (shared)
 auth_basic_module (shared)
 authn_file_module (shared)
 authz_default_module (shared)
 authz_groupfile_module (shared)
 authz_host_module (shared)
 authz_user_module (shared)
 autoindex_module (shared)
 cgi_module (shared)
 deflate_module (shared)
 dir_module (shared)
 env_module (shared)
 headers_module (shared)
 mime_module (shared)
 negotiation_module (shared)
 php5_module (shared)
 proxy_module (shared)
 proxy_ajp_module (shared)
 proxy_balancer_module (shared)
 proxy_connect_module (shared)
 proxy_ftp_module (shared)
 proxy_html_module (shared)
 proxy_http_module (shared)
 proxy_scgi_module (shared)
 reqtimeout_module (shared)
 rewrite_module (shared)
 setenvif_module (shared)
 status_module (shared)
Syntax OK

EDIT 2: I've been able to make some progresses. This is my VirtualHost:

    <VirtualHost *:80>
        ServerAdmin mymail@gmail.com
        ServerName asada.net
        ServerAlias www.asada.net
        DocumentRoot /var/www

        # NAS
        ProxyPreserveHost on
        ProxyPass /nas http://192.168.0.5
        ProxyPassReverse /nas http://192.168.0.5

    </VirtualHost>

Now, it somehow works... Web pages are loaded, but resources aren't. Let me post an screenshot:

Images, javascripts, css... aren't loaded

What can I do?

EDIT 3: Mate @Froggiz suggested me here some ways to achieve my goal, so I choosed what I thought it'd fit best my requirements: URL mapping. I discarded subdomains as my domain is free (noip.com).

Currently, my VirtualHost looks like the following piece of code:

    <VirtualHost *:80>
        ServerAdmin mymail@gmail.com
        ServerName asada.net
        ServerAlias www.asada.net
        DocumentRoot /var/www

        # NAS
        ProxyPreserveHost on
        ProxyPass /nas http://192.168.0.5
        ProxyPassReverse /nas http://192.168.0.5

        ProxyHTMLURLMap http://192.168.0.5 /nas

        <Location /nas>
            ProxyPassReverse /
            SetOutputFilter proxy-html
            ProxyHTMLURLMap http://192.168.0.5 /nas
            ProxyHTMLURLMap / /nas
            ProxyHTMLURLMap /nas /nas
            RequestHeader unset Accept-Encoding
        </Location>

    </VirtualHost>

Everything seem to be correctly setup. When I type asada.net/nas on my browser, the typical broken image is shown on the page:

broken image

And in the debugging console the is something weird: every resource which should be loaded gets a 'Failed to load resource: the server responded with a status of 404 (Not Found)' error. What's more weird is that the link of the resource (placed on the right) seems to be missing an '/' just after '...net/nas' and before 'web/...':

http://asada.net/nasweb/css/job_list_icon.css?v=WDV1.01

Look, my guess is that it should be

http://asada.net/nas/web/css/job_list_icon.css?v=WDV1.01

instead.

Any thoughts? Thank you again.

russellhoff
  • 129
  • 10
  • In fact css,js,img,... aren't loaded cause proxy don't redirect them as they aren't starting with /nas. There s many solution: easiest changing the way your server is on http://192.168.0.5 to server the nas on http://192.168.0.5/nas. Another way is here http://serverfault.com/questions/739372/apache2-reverse-proxy-url-mapping-will-not-apply?noredirect=1#comment924299_739372. Else you have to use the command substitute – Froggiz Dec 02 '15 at 09:45
  • @Froggiz sorry for not answering you faster. I guess it's possible to change it, but I'll consider that as the last option. I'll check the link you provided me. Thanks! – russellhoff Dec 02 '15 at 09:59

3 Answers3

1

What I understand is that the RPI serves pages for www.asada.net, and that is what you want. Next to that, you want to serve your NAS pages via www.asada.net/nas. I'm pretty sure that is possible, but I would suggest the following:

  • NAS: nas.asada.net (you may need to change the DNS for this to work)
  • RPI: www.asada.net, asada.net

This is much easier to handle and clearer too. I guess it won't matter much to you.

I think you need to setup Apache as proxyserver to your NAS. For this you need the module mod_proxy. You need to setup a vhost for the NAS, and inside that vhost you configure the proxy.

You don't tell us what kind of server you use. I guess it's Raspbian? Which version of Apache - 2.2 or 2.4? That can make a big difference so make sure you get the right tutorial, something like this:

How To Use Apache HTTP Server As Reverse-Proxy Using mod_proxy Extension

I see you tried to do this, but it doesn't work out. Give us the configuration you use. Let us know what you tried. We cannot help you without knowing what you did!

SPRBRN
  • 561
  • 4
  • 12
  • 27
0

You don't need mod_proxy or anything like that. You can do this with mod_alias, which is included in most default apache2 installs.

In your vhost config, or in an .htaccess file at the site root, simply add this line:

Redirect /nas http://192.168.0.5

You should do Redirect 301 /nas http://192.168.0.5 since it'll never be found at the .4 address. But it doesn't really matter for internal network stuff. A 301 redirect means "permanently moved".

Edit: More info on mod_alias and the redirect directive: https://httpd.apache.org/docs/2.4/mod/mod_alias.html#redirect

Edit 02: I skipped over this line in your question accidentally:

so that this page gets accessible from outside.

This is what I usually do in this scenario, as it's far simpler to troubleshoot and maintain than a reverse proxy. You'll need to move to hostname based addresses. We'll use nas.asada.net as SPRBRN suggested. Inside your network, set up DNS so that nas.asada.net points to 192.168.0.5. Outside your network, it should point to your external IP address.

On your NAS, change the access port to something non-default. I've never worked with one where you couldn't do this. Maybe use 8000 if using port 80, or 1443 if using 443. Have your firewall forward this new port for the NAS.

Then make the directive as follows:

Redirect 301 /nas http://nas.asada.net:8000[or whatever port your chose]

This way, access to your NAS isn't dependent on the raspberry pi or any other network device. Also, make sure you're using SSL if you plan to access it over the net.

Neil
  • 842
  • 6
  • 13
  • This hasn't worked for me. It simply redirects your request, e.g. if you type on your browser www.asada.net you get redirected to 192.168.0.5, which obviously doesn't work from outside your LAN. – russellhoff Dec 02 '15 at 09:15
  • My mistake, didn't read that line. I edited my answer to include what I usually do in this situation. If you like it, use it. If not, someone will surely provide alternatives. – Neil Dec 02 '15 at 09:27
  • 1
    Don't worry! I never vote down anybody ;) – russellhoff Dec 02 '15 at 09:28
0

First i explain you the trouble:

The commands

ProxyPass /nas http://192.168.0.5
ProxyPassReverse /nas http://192.168.0.5

will redirect http://asada.net/nas to http://192.168.0.5, so when you try to access an image it will be in the code http://192.168.0.5/example.gif, Apache server will not proxy the image as the request is /example.gif which not starts by /nas.

To solve this trouble there is many ways.

Changing path to fit proxy pattern

ProxyPass /nas http://192.168.0.5/nas
ProxyPassReverse /nas http://192.168.0.5/nas

like that all request will be correctly redirected when you access http://asada.net/nas. but in this cas nas need to be delivred from http://192.168.0.5/nas instead of http://192.168.0.5

Adding sub domain name

<VirtualHost *:80>
    ServerAdmin mymail@gmail.com
    ServerName asada.net
    ServerAlias www.asada.net
    DocumentRoot /var/www
</VirtualHost>

<VirtualHost *:80>
    ServerAdmin mymail@gmail.com
    ServerName nas.asada.net
    # NAS
    ProxyPreserveHost on
    ProxyPass / http://192.168.0.5
    ProxyPassReverse / http://192.168.0.5
</VirtualHost>

It is exactly the same solution as before but with another approach, / will fit with proxy / requests. And you will have a deddicated adress to access it. It will be more clean.

Using URL mapping

ProxyPass /nas/ http://192.168.0.5/
ProxyPassReverse /nas/ http://192.168.0.5/

ProxyHTMLURLMap http://192.168.0.5 /nas/

<Location /nas/>
   ProxyPassReverse /
   SetOutputFilter  proxy-html
   ProxyHTMLURLMap https://192.168.0.5 /nas/
   ProxyHTMLURLMap / /nas/
   ProxyHTMLURLMap  /nas/ /nas/
   RequestHeader    unset  Accept-Encoding
</Location>

as the answer in apache2 Reverse Proxy URL mapping will not apply

Using substitute

This i can't help sorry i still didn't found how to make substitute works

Froggiz
  • 3,013
  • 1
  • 18
  • 30
  • Thank you for your comment. My domain is a free one from www.noip.com, so there isn't any chance to create a subdomain (at least as I know). I'll test URL mapping. – russellhoff Dec 02 '15 at 10:24
  • I've implement your URL mapping approach, but still doesn't work... 2 comments. First one is that I must type asada.net/nas/ instead asada.net/nas (notice the trailing /). The second one is that resources still aren't loaded: GET http://asada.net/nas/web/function/home_usb.js?id=1449052247 asada.net/:1 – russellhoff Dec 02 '15 at 10:38
  • If I replace /nas/ by /nas, resources aren't found, see: http://asada.net/nasweb/function/system.js?id=1449053765 asada.net/:1. I've realised that there is one / missing between nasweb, it should be nas/web... – russellhoff Dec 02 '15 at 11:06
  • it doesn't work, sorry... – russellhoff Dec 02 '15 at 11:37
  • Let me study your approach a bit more ok? ty! – russellhoff Dec 02 '15 at 12:00
  • ok, i remove some comments to keep the post clean, and prevent being closed – Froggiz Dec 02 '15 at 12:02
  • Hi @Froggiz. I'll edit my question so that you can address in a clear way what I've done and what I guess it's not right yet. Thank you for your help. – russellhoff Dec 03 '15 at 12:05