sites-available
Nginx サイト設定ファイル
デフォルトでは、Nginx は /etc/nginx/sites-available/default
をデフォルトのサイト設定ファイルとして使用します。
必要に応じて、新しい設定ファイル(例:temp_api.conf
)を作成し、新しい API のリバースプロキシや静的リソースの提供を専用に設定することができます。
同様に、このファイルの内容を見てみましょう。
##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# https://www.nginx.com/resources/wiki/start/
# https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/
# https://wiki.debian.org/Nginx/DirectoryStructure
#
# In most cases, administrators will remove this file from sites-enabled/ and
# leave it as reference inside of sites-available where it will continue to be
# updated by the nginx packaging team.
#
# This file will automatically load configuration files provided by other
# applications, such as Drupal or Wordpress. These applications will be made
# available underneath a path with that package name, such as /drupal8.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##
# Default server configuration
#
server {
listen 80 default_server;
listen [::]:80 default_server;
# SSL configuration
#
# listen 443 ssl default_server;
# listen [::]:443 ssl default_server;
#
# Note: You should disable gzip for SSL traffic.
# See: https://bugs.debian.org/773332
#
# Read up on ssl_ciphers to ensure a secure configuration.
# See: https://bugs.debian.org/765782
#
# Self signed certs generated by the ssl-cert package
# Don't use them in a production server!
#
# include snippets/snakeoil.conf;
root /var/www/html;
# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html;
server_name _;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
# pass PHP scripts to FastCGI server
#
#location ~ \.php$ {
# include snippets/fastcgi-php.conf;
#
# # With php-fpm (or other unix sockets):
# fastcgi_pass unix:/run/php/php7.4-fpm.sock;
# # With php-cgi (or other tcp sockets):
# fastcgi_pass 127.0.0.1:9000;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# Virtual Host configuration for example.com
#
# You can move that to a different file under sites-available/ and symlink that
# to sites-enabled/ to enable it.
#
#server {
# listen 80;
# listen [::]:80;
#
# server_name example.com;
#
# root /var/www/example.com;
# index index.html;
#
# location / {
# try_files $uri $uri/ =404;
# }
#}
設定ファイルの概要
##
# Nginx 設定ファイルを完全に理解し、その力を最大限に引き出すために以下の URL をご覧ください。
# https://www.nginx.com/resources/wiki/start/
# https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/
# https://wiki.debian.org/Nginx/DirectoryStructure
#
# ほとんどの場合、管理者はこのファイルを `sites-enabled/` から削除し、
# `sites-available/` 内に参照用として残し、Nginx パッケージチームによって更新され続けます。
#
# このファイルは他のアプリケーション(例:Drupal や Wordpress)によって提供される設定ファイルを自動的に読み込みます。
#
# より詳細な例については、/usr/share/doc/nginx-doc/examples/ を参照してください。
##
この部分は開発者からの「コメント」で、Nginx 設定を学ぶためのリソースリンクが提供されており、管理者に対してこのファイルを sites-available/
に残し、実際の設定は sites-enabled/
から読み込むようにアドバイスしています。
開発者からのアドバイスですので、これに従い、ファイルには触れずに次へ進みましょう。
デフォルトサーバー設定
server {
listen 80 default_server;
listen [::]:80 default_server;
-
listen 80 default_server;
- サーバーが ポート 80(HTTP) をリッスンし、「デフォルトサーバー」として設定されます。
server_name
と一致しないリクエストは、このブロックで処理されます。
- サーバーが ポート 80(HTTP) をリッスンし、「デフォルトサーバー」として設定されます。
-
listen [::]:80 default_server;
- IPv6 のリクエストもポート 80 を介してこのサーバーにアクセスできるようにします。
-
デフォルトのウェブサイトルートディレクトリとインデックスページ設定
root /var/www/html;
# PHP を使用している場合は index.php をリストに追加
index index.html index.htm index.nginx-debian.html;root /var/www/html;
- サイトのルートディレクトリを設定します。すべての静的ファイル(HTML、CSS、JS)はこのディレクトリから提供されます。
index index.html index.htm index.nginx-debian.html;
- デフォルトのインデックスファイルを設定します。
index.html
が存在する場合はそれが返され、存在しない場合はindex.htm
を探し、どちらもなければlocation
ルールに従ってリクエストを処理します。
- デフォルトのインデックスファイルを設定します。
-
サーバー名設定
server_name _;
server_name _;
_
は「すべての一致しないリクエスト」をこのサーバーブロックで処理することを意味します。- 特定のドメイン名に変更することもでき、例えば
server_name example.com;
と設定すると、example.com
のリクエストだけがこの設定で処理されます。
-
静的リソースと 404 ページの処理
location / {
# 最初にファイルとしてリクエストを処理し、次に
# ディレクトリとして処理し、それが失敗した場合は 404 を表示
try_files $uri $uri/ =404;
}location / {}
- ルート(
/
)のリクエストを処理する方法を設定します。 try_files $uri $uri/ =404;
- 順番に試行します:
$uri
(リクエストされたファイル、例えば/index.html
)。$uri/
(もしリクエストがディレクトリの場合、その中にindex.html
があるかチェック)。- どちらも存在しない場合、404 Not Found を返します。
FastCGI PHP 処理
このコードブロックはデフォルトでコメントアウトされています。
# PHP スクリプトを FastCGI サーバーに渡す
#
#location ~ \.php$ {
# include snippets/fastcgi-php.conf;
#
# # php-fpm(または他の Unix ソケット)で処理する場合:
# fastcgi_pass unix:/run/php/php7.4-fpm.sock;
# # php-cgi(または他の TCP ソケット)で処理する場合:
# fastcgi_pass 127.0.0.1:9000;
#}
このブロックは FastCGI 設定 で、Nginx が PHP リクエストを処理し、PHP-FPM サーバーに転送します。
- PHP-FPM 処理を有効にする方法:
location ~ \.php$
のコメントを解除します。- PHP-FPM が
/run/php/php7.4-fpm.sock
または127.0.0.1:9000
で実行されていることを確認します。
.htaccess
アクセス制限
このコードブロックはデフォルトでコメントアウトされています。
# Apache のドキュメントルートと nginx のドキュメントルートが一致する場合、
# .htaccess ファイルへのアクセスを拒否
#
#location ~ /\.ht {
# deny all;
#}
.htaccess
は Apache で使用される設定ファイルで、Nginx は .htaccess
をサポートしていません。
ただし、Nginx と Apache が共存している場合、このルールを使用して .htaccess
への外部アクセスを防ぐことができます。
仮想ホスト設定の例
このコードブロックはデフォルトでコメントアウトされています。
#server {
# listen 80;
# listen [::]:80;
#
# server_name example.com;
#
# root /var/www/example.com;
# index index.html;
#
# location / {
# try_files $uri $uri/ =404;
# }
#}
これは example.com
サイト用の仮想ホスト設定の例です。
このコードを sites-available/example.com
に移動し、sites-enabled/
にシンボリックリンクを作成して有効にできます。
設定を有効/無効にする方法
sites-available
内で設定ファイルを作成した後、このファイルを sites-enabled
にリンクして初めて設定が反映されます。
例えば、この default
設定ファイルを Ubuntu/Debian システムで有効にするには、次のコマンドを使用します:
sudo ln -s /etc/nginx/sites-available/default /etc/nginx/sites-enabled/
無効にするには、まず sites-enabled
内のリンクを削除します:
sudo rm /etc/nginx/sites-enabled/default
その後、Nginx をリロードします:
sudo systemctl reload nginx
この default
設定ファイルは通常 Nginx のデフォルトサイト として使用され、Nginx が正常に動作しているかをテストするのに適しています。
サイトをカスタマイズする場合は、「新しい設定ファイルを作成し」、適切な server_name
設定を有効にすることをお勧めします。