1 2 3 4 5 |
location [ = uri | ^~ uri | ( ~ | ~* ) uri | uri ] { ...; ...; ...; } |
directory www.mg-comp.com
../www/index.html
../www/page.php
../www/favicon.ico
../www/.metadata
../www/admin/admin.php
../www/doc/document.html
../www/doc/img/image2.gif
../www/doc/photo/photo1.jpg
../www/doc/photo/1/photo2.jpg
../www/doc/photo/m/photo3.jpg
../www/document/readme.txt
../www/img/image1.gif
other site user.mg-comp.com
server {
listen 443;
server_name user.mg-comp.com;
access_log /log/user.log logtype_user;
index index.php;
location ~* (.*) { rewrite $scheme://$host/; }
fastcgi_pass cgibin;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
#nginx configuration #server directive server { listen 80; server_name www.mg-comp.com; index index.html; #完全一致(優先度高) #location = (対照式){ ...; } location = / { root /www }; location = /favicon.ico { access_log off; } location = /robots.txt { return 200 "User-agent: *\nDisallow: /\nAllow: /doc/"; location = /admin/ { arrow 127.0.0.1; deny all; } location = /user/ { return 301 $scheme://user.mg-comp.com/?$remote_address; } #前方一致(優先度高) #location ^~ (対照式){ ...; } location ^~ /doc { index readmme.txt; } location ~* ^/documents/ { #dontwork }; location ^~ /.*\.php$ { fastcgi_pass cgibin; #1 dontwork} location ^~ /page.php { fastcgi_pass cgibin; #1} #正規表現(優先度中) #location ~* (対照式){ ...; } location ~* ^/doc/.*\.jpg$ { expires 30d; } location ~* ^/doc/[^/]*.\jpg$ { expires max; } location ~* /photo/(.*)/.*\.jpg { return 200 "$1=m"; } #単純一致(優先度低) #location (対照式){ ...; } location \.gif$ { expires max; } location / { index page.php; } locaton /\. { return 404; } } |
#1 前方参照で最長一致が優先される表現(正規表現は不可)
完全一致としているものよりも限定した使い方となる
#sample
1 2 3 4 5 6 7 8 9 10 11 12 13 |
server { location = /wp-login.php$ { access_log /log/wplogin.log; return 200 break; } location = /admin/ { limit_req zone=ipblock burst=3; rewrite $scheme://$host/wp-login.php; } location ~^ /.*\.php$ { fastcgi_pass cgibin; } } |