I have a bunch of subdomains in one single server:
a.example.com
b.example.com
news.example.com
All of them are in the same Apache virtualhost.
I need to use a feed provided by the news subdomain inside the a and b subdomain. The feeds typically look like this:
news.example.com/news/a
news.example.com/news/b
On the a and b subdomains, I'm using jquery's ajax function to load the data from the news feeds, and present it on a and b. This initially didn't work, because of the same-origin policy.
I was able to override this by adding the Access-Control-Allow-Origin "*" to my Apache config file.
... but this works only in Firefox, Chrome and Safari. Internet explorer seems to ignore that directive.
Thus, I need to create a proxy.
What I need is a new directory in all my subcomains (for example /proxy) that Apache detects, and redirects to news.example.com, no matter what the subdomain. So:
a.example.com/proxy/news/a -> return the contents of news.example.com/news/a
b.example.com/proxy/news/b -> return the contents of news.example.com/news/b
Can I do this directly in Apache + submodules (for example, mod_rewrite), or do I need to use a scripting language like PHP for doing this?
You want the ProxyPass Directive.
ProxyPass /proxy/news/a http://news.example.com/news/a
At the end we were able to make the proxy using a combination of two modules: mod_rewrite and mod_proxy.
The syntax was the following:
rewriteEngine on
rewriteRule proxy/(.+)$ http://news.example.com/$1 [P]
The [P] at the end is telling the rule "act as a proxy" and doesn't work without mod_proxy. Without it, apache makes a "redirect" (the url at the top of the page changes) instead of "just serving the page".
Apache can be configured to use apache:
consider this working sample code (proxy part):
<VirtualHost *:80>
ServerAdmin webmaster#localhost
ServerName ci.testserver.com
ServerAlias ci
ProxyRequests Off
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
ProxyPreserveHost on
ProxyPass / http://localhost:8080/
</VirtualHost>
Related
I have this strange problem that I can best describe as "namespace leakage". I have setup a vagrant box running Apache2 with VHosts setup to replicate the production server in terms of domains & sub domains. I have edited my local machine's hosts file accordingly, and my VHosts work fine.
I have also created a skeleton/base app coded with PhalconPHP. It's a multi modular app with all the generic features I require to develop my apps (actually redevelop a load of very old, outdated apps). The skeleton app works fine.
The problem I have is that if I go to app1.dev in my browser, it works. Then if I go to app2.dev, app2 is clearly trying to load some components - views etc from app1 and is giving errors. However, close the browser and try again by going to app2.dev and it now works fine. Then go to app1.dev and that is now broken and trying to load components from app2! I managed to track this odd behaviour down to namespace collision.
It has to be namespace collision because my apps are all based on the skeleton app and use it's name spaces, which are obviously the same for generic parts of the app - modules such as App\backend, App\frontend etc. If on a broken app, I navigate in my browser to a part of that app that is unique, and therefore has a unique namespace, it works fine because there is no collision! Also a couple of apps are coded with Codeigniter3 which does not use namespaces, and those apps do not have this issue. It only effects the Phalcon apps with namespaces.
I will add that each app uses .htaccess to direct requests to the front controller located in public/ directory.
Options FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ public/ [L]
RewriteRule (.*) public/$1 [L]
</IfModule>
I wondered if the .htaccess was the issue, but I think I've ruled that out. It is doing what it is supposed to do.
Here's an example of one of my VHost settups in apache - they all follow this pattern.
<VirtualHost *:80>
ServerName app1.dev
ServerAlias www.app1.dev
DocumentRoot /vagrant/www/app1
<Directory "/vagrant/www/app1">
Options Indexes Followsymlinks
AllowOverride All
Order allow,deny
Allow from All
Require all granted
</Directory>
</VirtualHost>
Changing all the namespaces throughout every app would be a pretty major job, and I don't think that should be necessary. I don't think this should be an issue on the production server as that uses CloudLinux/Centos & WHM, but it is a bit of a worry!
Clearly namespaces should not collide across different document routes and VHosts right? What am I missing?
I have solved similar problem in Apache using different local IP's for every app:
/etc/hosts
127.1.0.1 edu.l
127.1.1.1 tech.l
...
/etc/apache2/sites-available/001-blogs.conf
<VirtualHost edu.l:80>
ServerName edu.l
ServerAdmin webmaster#localhost
DocumentRoot /var/www/blog/edu
ErrorLog ${APACHE_LOG_DIR}/edu.error.log
CustomLog ${APACHE_LOG_DIR}/edu.access.log combined
</VirtualHost>
<VirtualHost tech.l:80>
ServerName tech.l
ServerAdmin webmaster#localhost
DocumentRoot /var/www/blog/tech
ErrorLog ${APACHE_LOG_DIR}/tech.error.log
CustomLog ${APACHE_LOG_DIR}/tech.access.log combined
</VirtualHost>
...
You can ommit configuring names in hosts ans use IP's in .conf tho.
It works because if you define few apps on same IP and port, Apache seems to remember directory it is working on and mismatching files.
I have the following problem.
I have two domains: www.domain1.de and www.domain2.de
I also have path is on Jelastic server where to find my PHP page
myphpsite.jelastic.dogado.eu.
Now I wanted to do the following.
1) If I go to www.domain1.de, then should address bar of the Web Browser
www.domain1.de be displayed, but the page is fetched from myphpsite.jelastic.dogado.eu.
2) When I go to www.domain2.de, then should address bar of the Web Browser
www.domain2.de be displayed, but the page is fetched from myphpsite.jelastic.dogado.eu / admin /.
so
1) www.domain1.de -> myphpsite.jelastic.dogado.eu
2) www.domain2.de -> myphpsite.jelastic.dogado.eu / admin /
The first one I can do by CNAM Record
But we can I solve the second problem without frames?
thank you
This is just a matter of configuring Apache VirtualHosts (assuming that you're using Apache), or Nginx Server Blocks (if you're using Nginx). Leo's link can help you with either of them, within a Jelastic context (where to find those config files etc.): http://docs.jelastic.com/multiple-domains-php
Here's a quick example for Apache:
<VirtualHost *:80>
DocumentRoot /var/www/webroot/ROOT
ServerName domain1.de
ServerAlias www.domain1.de
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /var/www/webroot/ROOT/admin
ServerName domain2.de
ServerAlias www.domain2.de
</VirtualHost>
You might also wish to define different log files for each domain etc.
There are many possibilities, and since Jelastic gives you direct access to the server config. files you can configure almost anything that you could want here. It's just a matter of reviewing the Apache (or Nginx) docs and trying it out.
Notes:
Beware that the Jelastic default configuration defines a wildcard to catch all requests. You need to place your custom configuration before that (either literally, or via an include), or else overwrite the default VirtualHost configuration block - otherwise your config. will not have any effect.
The above example handles HTTP only. If you need to handle HTTPS, you need to configure this separately. (e.g. :443 instead of :80)
Remember that you need to restart Apache (or Nginx) for config. changes to take effect.
See also:
Apache VirtualHost Documentation
Nginx Server Blocks Documentation
I have a Synology DSM set up for home hosting, and I've been trying to play around with it. My current goal is to assign subdomains to specific services, e.g. the DSM Admin panel, SickBeard, etc.
The only problem with this is the way how Synology manages its services. Some of them are simply webhost-based (and put into the /var/services/web/ folder in their own subfolder), and some of them are running on separate ports (e.g. Sickbeard runs on port 8083, while the admin panel runs on port 5000).
The results are want are quite simple: redirect set subdomains (see below) to these ports, WITHOUT displaying the ports.
admin.fonix232.net -> fonix232.net:5000
sb.fonix232.net -> fonix232.net:8083
home.fonix232.net -> fonix232.net:8085
etc.
I've set up a simple mod_rewrite rule for the subdomain admin.fonix232.net, in the following way (in httpd-vhost.conf-user):
<VirtualHost *:*>
ServerName admin.fonix232.net
RewriteEngine on
RewriteCond %{HTTP_HOST} admin\.fonix232\.net [NC]
RewriteRule ^/(.*)$ http://fonix232.net:5000/$1 [P,QSA,L]
</VirtualHost>
It works fine, except it does not rewrite the port - it stays 5000, but at least on the subdomain.
How could I remove it?
Solution was pretty simple, I had to use mod_proxy and mod_proxy_http.
Thanks to the way how Synology built its Apache system (three separate services for system/webman, user and webdav), the best solution was to create a new config file and extend the existing by slightly modifying the startup script to import that too (for some mysterious reasons, any kind of manual addition to config files gets erased as soon as someone edits the settings from webman).
So after that, I had to create separate VirtualHosts for the separate services I wanted to redirect, the following way:
<VirtualHost *>
ServerName [full subdomain you want to redirect to]
ProxyRequests On
ProxyPreserveHost On
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
ProxyPass / [target address and port you want to reach]
ProxyPassReverse / [target address and port you want to reach]
<Location />
Order allow,deny
Allow from all
</Location>
While most of the tutorials show that ProxyRequests should be Off, for me it never worked that way, only by enabling it.
Now one thing remains, the exact reverse. Meaning, if port X is called from *.domain.tld, no matter which, drop me on the assigned subdomain.domain.tld. Harder nut to crack, but getting there.
all. So I'm running Apache 2.2. I have a single VirtualHost that's used for a Django application (by way of mod_wsgi) as well as a PHP one that lives in a subdirectory. Normally, this is no problem. You can just Alias /subdir /path/to/phpapp followed by WSGIScriptAlias / /path/to/django.wsgi.
But, the complication is that this PHP application uses mod_rewrite to implement "Fancy URLs," which just means that '/subdir/foo/bar' will get rewritten to something like '/subdir/index.php?path=foo/bar'.
Here's my configuration:
<VirtualHost *:80>
ServerName [snip]
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
Alias /phpapp/ /home/ubuntu/phpapp/
<Directory /home/ubuntu/phpapp>
Order allow,deny
Allow from all
RewriteEngine on
RewriteCond $1 !^(index\.php|img|bin)
RewriteRule ^(.*)$ index.php?__dingo_page=$1 [PT]
</Directory>
WSGIDaemonProcess foo user=ubuntu
WSGIProcessGroup foo
WSGIScriptAlias / /home/ubuntu/djangoapp/apache/django.wsgi
<Directory /home/ubuntu/djangoapp/apache>
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
The problem is that whenever a rewrite takes place (e.g., I try to go to /phpapp or /phpapp/foo) the request gets handled by WSGI and I see my Django site's 404 page. If the address passes through (e.g., I go to /phpapp/index.php) then it is handled by PHP and works normally. I thought maybe that adding the [PT] flag to my RewriteRule would fix this problem, but it doesn't seem to have any effect on which handler is chosen. I've also tried SetHandler application/x-httpd-php in the Directory section for the PHP app.
I need the Django application to continue to handle any URLs that aren't specifically aliased to something else. There must be a way to make this work! Thanks.
Using /phpapp wouldn't ever work because you have a trailing slash on that path for the Alias directive. Use:
Alias /phpapp /home/ubuntu/phpapp
By rights that Alias directive should then take precedence over WSGIScriptAlias for the sub URL.
I would suggest you enable logging by mod_rewrite and verify what the URL is being written to. So long as the rewritten URL still sits below /phpapp, it should be fine.
The alternative is to not use WSGIScriptAlias, but use the scheme as outline towards the end of:
http://code.google.com/p/modwsgi/wiki/ConfigurationGuidelines#The_Apache_Alias_Directive
That allows you to set things up so that the Python web application will only be used as a fallback if no static resource, including PHP, could be mapped.
I have a Django application hosted on my main domain (example.com), and I now need to host a PHP application on a subdomain (forum.example.com).
In the directory of the main domain, I have the following .htaccess entries:
SetHandler mod_python
PythonPath "/home/.../apps/example'] + sys.path"
PythonOption mod_python.importer.path "['/home/vlive/python']+ sys.path"
PythonHandler django.core.handlers.modpython
#PythonDebug On
SetEnv DJANGO_SETTINGS_MODULE example.settings
SetEnv PYTHON_EGG_CACHE /tmp/egg-cache
At present, when I load the subdomain (forum.example.com) I still see the main site (example.com).
How can I fix this?
You need to set up your subdomains via the VirtualHost directive and only add the mod_python / mod_wsgi handler in one of the VirtualHosts.
You said you loaded the subdomain and it still shows the main site. Would you mind showing us your Apache's site configuration?
The most common reason for wrong VirtualHost being selected is a missing NameVirtualHost directive that matches host/port qualifier specified in VirtualHost directive.
What NameVirtualHost directive do you have set? What is the argument to the two VirtualHost configurations?
The general layout would be:
NameVirtualHost *:80
<VirtualHost *:80>
ServerName example.com
...
</VirtualHost>
<VirtualHost *:80>
ServerName forum.example.com
...
</VirtualHost>
These would usually be in the same file for a Linux Apache distribution though.
What have you used?
As Graham noted, most probably you have wrong VirtualHost configuration.
Double check that there is two different VH with right ServerName and that you don't use * (wildcard) in ServerName and ServerAlias that may overlap with subdomain or it goes AFTER subdomain.
Apache only search for first VH that matches, so if you have something like *.example.com above any other subdomain like forum.example.com would not work.
Also mod_wsgi is recommended by django documentation, consider switching to it.