NGINX設定ファイル
NGINXをリバースプロキシまたはウェブサーバとして使用するための設定ティップス。サーバセキュリティ、ロードバランサーとしての機能も併せ持つため、サーバ導入時には欠かせないオープンソースのサーバアプリです。
NGINXをリバースプロキシまたはウェブサーバとして使用するための設定ティップス。サーバセキュリティ、ロードバランサーとしての機能も併せ持つため、サーバ導入時には欠かせないオープンソースのサーバアプリです。
以下の例では、ロケーションで指定したディレクトリ内で、URIで表示された文字列を、まず初めにファイルとして検索、そのファイルが存在しない場合にはフォルダとして検索します。結果が一致した時点でその内容を返し、共に一致しなければ404を返します。
location / {
try_files $uri $uri/ $uri.html =404;
}
ファイル、フォルダ名共にURIと一致しない場合、以下のようにプロキシーサーバに飛ばすことも出来ます。
location / {
try_files $uri $uri/ @backend;
}
location @backend {
proxy_pass http://backend.example.com;
}
Regular Expression正規表現参考
NGINX uses Perl Compatible Regular Expressions (PCRE).
http://nginx.org/en/docs/http/ngx_http_core_module.html#alias
rootディレクティブ
location /static/ {
root /var/www/app/;
autoindex off;
}
rootで指定したディレクトリにlocationのディレクトリが追加されます。
/var/www/app/static
aliasディレクティブ
location /static/ {
alias /var/www/app/static/;
autoindex off;
}
locationのディレクトリがaliasのディレクトリを表します。
/var/www/app/static
https://www.nginx.com/resources/wiki/start/topics/examples/phpfcgi/
/etc/nginx/fastcgi_params
ファイル内で以下設定項目を確認すること。特にphpスクリプトを実行する上で、以下の設定がポイントとなります。(404が返ってきた場合はここを見直します)
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx/$nginx_version;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
fastcgi_param HTTPS $https;
# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param REDIRECT_STATUS 200;
fastcgi_paramの設定については以下も参考にして下さい。
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
or
fastcgi_param SCRIPT_FILENAME $request_filename;
You get the request /info/
and have the following configuration:
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /home/www/scripts/php$fastcgi_script_name;
SCRIPT_FILENAME
would equal /home/www/scripts/php/info/index.php
, but using $request_filename
it would just be /home/www/scripts/php/info/
.
The configuration of fastcgi_split_path_info
is important as well. See here for further help: https://nginx.org/en/docs/http/ngx_http_fastcgi_module.html#fastcgi_split_path_info
ヘッダー読み取りエラー
デフォルトでNginxはアンダースコア"_"を含むヘッダーは読み込まない。
Nginxの設定ファイルに “underscores_in_headers on;” を追加。
underscores_in_headers
https://nginx.org/en/docs/http/ngx_http_core_module.html#underscores_in_headers
Ex)
upstream foo_app {
server 127.0.0.1:3000;
}
server {
listen 80;
# listen [::]:80 default_server ipv6only=on;
root /home/user/foo/foo_web/public;
server_name foo.it www.foo.it;
underscores_in_headers on;
client_max_body_size 4g;
location ~ ^/(assets)/ {
gzip_static on;
expires max;
add_header Cache-Control public;
}
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_connect_timeout 300;
proxy_read_timeout 300;
proxy_pass_request_headers on;
proxy_pass http://foo_app;
}
}
NginxにおけるApacheのgetallheaders()代替案
https://www.php.net/manual/en/function.getallheaders.php#84262
<?php
if (!function_exists('getallheaders'))
{
function getallheaders()
{
$headers = [];
foreach ($_SERVER as $name => $value)
{
if (substr($name, 0, 5) == 'HTTP_')
{
$headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value;
}
}
return $headers;
}
}
?>
https://www.nginx.com/resources/wiki/modules/auth_digest/
標準モジュールではないため、別途インストールが必要。
/etc/nginx/conf.d/dfault.conf内のlocationブロックで以下指定
client_max_body_size 20M;
server {
listen 443 ssl default_server;
listen [::]:443 ssl default_server;
root /var/www/html;
index index.php index.js index.html index.htm index.nginx-debian.html;
server_name domain.com www.domain.com;
ssl_certificate /ssl/domain.com.chained.crt;
ssl_certificate_key /ssl/domain.com.key;
location / {
proxy_pass http://localhost:8000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
location /wordpress {
proxy_pass http://localhost:8090;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
例1) mapモジュールによるリダイレクト
map により /old.html を /index.html に対応させ、それが真(1)の時にリダイレクト
. . .
# Default server configuration
#
# Old website redirect map
#
map $uri $new_uri {
/old.html /index.html;
}
server {
listen 80 default_server;
listen [::]:80 default_server;
# Old website redirect
if ($new_uri) {
rewrite ^ $new_uri permanent;
}
. . .
例2) mapによる国別のサイトへのアクセス制限
以下の設定ファイルを作成し、国データを指定。
/etc/nginx/conf.d/geoip.conf
. . .
# GeoIP database path
#
geoip_country /usr/share/GeoIP/GeoIP.dat;
制限する国コードを指定(AFとAL)
. . .
# Default server configuration
#
# Allowed countries
#
map $geoip_country_code $allowed_country {
default no;
AF yes;
AL yes;
}
# Old website redirect map
#
map $uri $new_uri {
/old.html /index.html;
}
server {
listen 80 default_server;
listen [::]:80 default_server;
# Disallow access based on GeoIP
if ($allowed_country = no) {
return 444;
}
# Old website redirect
if ($new_uri) {
rewrite ^ $new_uri permanent;
}
. . .
サーバディレクトリ毎に設定を細かく指定する Location(Nginxコアモジュール) についてのメモ。
Nginxコアモジュール
Location
Syntax: location [ = | ~ | ~* | ^~ ] uri { ... }
location @name { ... }
Default: —
Context: server, location
= 一致した場合
~ 大文字・小文字を区別(正規表現)
~* 大文字・小文字を区別しない(正規表現)
^~ 一致したら正規表現(~,~*)の条件を適用しない
@name 他の設定からのリダイレクト先を名前により指定する場合に使用
ディレクトリが / の場合、configuration Aを適用
location = / {
[ configuration A ]
}
ディレクトリが / を含む場合、configuration Bを適用
location / {
[ configuration B ]
}
ディレクトリが /documents/ を含む場合、configuration Cを適用
location /documents/ {
[ configuration C ]
}
ディレクトリが /images/ を含む場合、configuration Dを適用
location ^~ /images/ {
[ configuration D ]
}
ファイル名に gif.jpg,jpeg の拡張子を含む場合、configuration Eを適用
location ~* \.(gif|jpg|jpeg)$ {
[ configuration E ]
}