3

I just created a new structure for a website, and I need some help on redirecting to new places.

The code that works is here, but I guess this can be optimized a lot.

location = /nl {
    rewrite ^ $scheme://$server_name permanent;
}

location = /nl/ {
    rewrite ^ $scheme://$server_name permanent;
}

location = /en {
    rewrite ^ $scheme://$server_name permanent;
}

location = /en/ {
    rewrite ^ $scheme://$server_name permanent;
}

location = /nl/contact {
    rewrite ^ $scheme://$server_name/contact/ permanent;
}

location = /en/contact {
    rewrite ^ $scheme://$server_name/contact/ permanent;
}
3
  • Rewrites don't have to be in location blocks - and can match against a regex. Very roughly (untested, not fully thought through, and regex isn't my forte) you may have some success with two rewrites: rewrite ^/(en|nl)/contact$ $scheme://$server_name/contact/ permanent; and rewrite ^/(en|nl)(/.*)$ $scheme://$server_name permanent; order does matter in this case. Alternatively, you can combine the first 4 and last 2 location blocks with regex matches - but that does chance the matching priority (which may not be very significant).
    – cyberx86
    Dec 20, 2011 at 6:44
  • Nice, it actually works without the location blocks. I had to change the code a little tho, the final working code is: rewrite ^/(en|nl)(/?)$ $scheme://$server_name permanent; rewrite ^/(en|nl)(/contact)(/?)$ $scheme://$server_name/contact/ permanent; Can you add this as an answer so I can close this question. Dec 20, 2011 at 21:08
  • Added as an answer - thanks for the improvements (the /.* was a combination of carelessness (missing the ?) and a misinterpretation (that all files below the directory should be rewritten); but I don't think I would have caught the trailing slash on contact). You may not need /contact in brackets though - which might improve performance a tad.
    – cyberx86
    Dec 20, 2011 at 21:37

1 Answer 1

0

Rewrites don't have to be in location blocks - and can match against a regex.

You may have some success with two rewrites:

rewrite ^/(en|nl)(/contact)(/?)$ $scheme://$server_name/contact/ permanent;
rewrite ^/(en|nl)(/?)$ $scheme://$server_name permanent;

Alternatively, you can combine the first 4 and last 2 location blocks with regex matches - but that does chance the matching priority (which may not be very significant).

(Credit to @Saif Bechan for fixing my original proposition)

7
  • Thanks again for the answer. By the way, I have been looking at your, and are you the ownser of the website? I actually used the article on how to set up php-fpm with nginx properly on an EC2 instance. Really good. I have one question tho, do you have any recommendation on how to set up the nginx / php-fpm environment in terms of users and file permissions. I now run an environment where sites are located in /home/myuser/sites and both nginx and php-fpm have group and owner set to myuser which is completely unsafe. Still devel environment tho, no public access. Dec 20, 2011 at 21:41
  • If you are referring to the site linked in my profile, the yes, I am its owner (and it does run on EC2). Glad someone found that article useful - it is a bit outdated now that php-fpm is available from the amzn repository though. My setup has users and sites. /var/www/users/{user_name}/{domain} (and for simplicity, /var/www/{domain} is symlinked. Under each {domain} is a /public_html folder. Each {domain} runs as its own php-fpm user (e.g. web1, web2, etc.). Files are owned by the php-fpm user, and group is set to the client (e.g. web1:cyberx86); Permissions are 644.
    – cyberx86
    Dec 20, 2011 at 21:55
  • I was referring to link in your profile. Saw an interesting article on apachebench also, will check that out in the next few weeks. Ok the looks quite good, I think I will implement the same system. One thing, what is the setup for nginx? Maybe you can write an article on this subject, because I haven't seen many of those around. And since it is quite a big step to do a proper chroot with nginx and php-fom this setup will be ideal for users. Dec 20, 2011 at 22:10
  • Just to add a couple more points here - each php-fpm pool listens on a different port; allowed-clients is restricted to localhost only; there is a php_open_basedir restriction in effect; and suhosin is installed; you should also chroot each pool. I do try to improve things whenever I come across something new and interesting - but so far, I haven't had any problems with it.
    – cyberx86
    Dec 20, 2011 at 22:11
  • I will certainly think about it - I enjoy playing around with servers - but my notes don't always make it into an article (I still have notes on building a server cluster from the summer that are sitting on my computer). I am tempted to try out a more formal benchmark on varnish vs. nginx - but I'll certainly add an article on nginx to my todo list as well.
    – cyberx86
    Dec 20, 2011 at 22:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .