I'm currently trying to break apart 3 applications from one repository into 3, but keeping the url structure, so basically different locations under the same domain have to be delivered by different applications.
What I'm struggling with is that one of the apps needs to be the fallback for non-existent urls, so if the first one doesn't match, and the second one doesn't, then the third should handle the request
The structure I've got is:
/etc/nginx/sites-enabled/main_site, in here, apart from server_name and logs I've got include /etc/nginx/subsites-enabled/*
, where I've got 3 configuration files, one for each of the apps.
Each of the 3 config files contains a location block.
I've tried negative lookahead in regex (basically trying to hardcode the urls the other apps handle) but failed.
So, to summarise:
/ and /community should be delivered by /etc/nginx/subsites-enabled/example.org/home (a few perl scripts)
/news should be delivered by /etc/nginx/subsites-enabled/example.org/news (wordpress)
everything else should be delivered by /etc/nginx/subsites-enabled/example.org/app (cake app)
The perl bit works fine. The problem I'm having is that the app is taking over news (probably because it matches .*), I've tried various options (I've been at this for 2 days) but none of them solved all the problems (sometimes static assets wouldn't work, etc).
My configuration is:
/etc/nginx/sites-enabled/example.org:
server {
listen 80;
server_name example.org;
error_log /var/log/nginx/example.org.log;
include /etc/nginx/subsites-enabled/example.org/*;
}
/etc/nginx/subsites-enabled/example.org/home:
location = / {
rewrite ^.*$ /index.pl last;
}
location ~* /community(.*) {
rewrite ^.*$ /index.pl last;
}
location ~ \.pl {
root /var/www/vhosts/home;
access_log /var/log/nginx/home/access.log;
error_log /var/log/nginx/home/error.log;
include /etc/nginx/fastcgi_params;
fastcgi_index index.pl;
fastcgi_param SCRIPT_FILENAME /var/www/vhosts/home$fastcgi_script_name;
fastcgi_pass unix:/var/run/fcgiwrap.socket;
}
/etc/ngins/subsites-enabled/news
location /news {
access_log /var/log/nginx/news/access.log;
error_log /var/log/nginx/news/error.log debug;
error_page 404 = /news/index.php;
root /var/www/vhosts/news;
index index.php;
if (!-e $request_filename) {
rewrite ^.*$ /index.php last;
}
location ~ \.php {
include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/vhosts/news$fastcgi_script_name;
}
}
/etc/nginx/subsites-enabled/app:
location ~ .* {
access_log /var/log/nginx/app/access.log;
error_log /var/log/nginx/app/error.log;
rewrite_log on;
index index.php;
root /var/www/vhosts/app/app/webroot;
if (-f $request_filename) {
expires 30d;
break;
}
if (!-e $request_filename) {
rewrite ^.*$ /index.php last;
}
location ~ \.php {
include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/vhosts/app/app/webroot$fastcgi_script_name;
}
}
@
prefix) that maps to your default app. You can also setup an error_page that maps a 404 to a named location.location ^~ /news
. b) for your app block, you should be able to dolocation /
(this is not the same aslocation = /
, but should match everything not already matched. c) in some cases (particularly regexes), order does matter - you may want to combine the 3 files into a single file with the blocks in the right order. Also, use try_files instead of!-e
. Finally see wiki.nginx.org/HttpCoreModule#location.