Nginxのstatic fileとreverse proxy1 / 3
Serverとlocation
Nginxはmain/events/http、http内のserver、server内のlocation等のcontextで設定する。listenは受けるsocket、server_nameはhost、rootはfile pathの基準、indexはdirectoryで使う候補fileへ関わる。/etc/nginx/配下のincludeや配布元の分割設定を読む。
events { worker_connections 1024; }
http {
server {
listen 80;
server_name www.example.test;
root /srv/www/example;
index index.html;
location /static/ {
try_files $uri =404;
}
location /api/ {
proxy_pass http://127.0.0.1:8080/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}
}
これはbackendがlocal8080でHTTPを受け、static fileのpathと権限を用意した例である。root方式の/static/logo.pngは/srv/www/example/static/logo.pngへ対応する。alias方式はlocationに対応するpathを置き換えるため、rootと同じ連結を仮定しない。