Nginx のインストール
Nginx をインストールするには、以下のようにします。
sudo dnf install nginx
Nginx の状態を確認したり起動するには、以下のようにします。
# 起動
sudo systemctl start nginx
# nginx の再起動
sudo systemctl restart nginx
# nginx の状態の確認
sudo systemctl status nginx
# 自動起動の有効化
sudo systemctl enable nginx
ファイアウォールの設定
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
設定ファイル
設定ファイルは「 /etc/nginx 」の中にあります。独自の設定ファイルを作成するには、「 conf.d 」の中に作成します。
設定ファイルの文法エラーをチェックするには、以下のようにします。
nginx -t
nginx の設定ファイルの中は以下のようになっています。
/etc/nginx/nginx.conf
# nginx version: nginx/1.20.1
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 4096;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
server {
listen 80;
listen [::]:80;
server_name _;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
error_page 404 /404.html;
location = /404.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
# Settings for a TLS enabled server.
#
# server {
# listen 443 ssl http2;
# listen [::]:443 ssl http2;
# server_name _;
# root /usr/share/nginx/html;
#
# ssl_certificate "/etc/pki/nginx/server.crt";
# ssl_certificate_key "/etc/pki/nginx/private/server.key";
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 10m;
# ssl_ciphers PROFILE=SYSTEM;
# ssl_prefer_server_ciphers on;
#
# # Load configuration files for the default server block.
# include /etc/nginx/default.d/*.conf;
#
# error_page 404 /404.html;
# location = /40x.html {
# }
#
# error_page 500 502 503 504 /50x.html;
# location = /50x.html {
# }
# }
}
アクセスログやエラーログは、以下にあります。
sudo less /var/log/nginx/error.log
sudo less /var/log/nginx/access.log
html ファイルのアップロード先を変更する
「 /var/www/html 」に htmlファイルのアップロード先を変更します。
「 /etc/nginx/conf.d 」の中に「 myapp.conf 」というファイルを作成します。
sudo vi /etc/nginx/conf.d/myapp.conf
nginx の設定ファイルを以下のように変更します。
/etc/nginx/conf.d/myapp.conf
server {
location / {
root /var/www/html;
index index.html;
}
}
「 /var 」の中に「 www/html/index.html 」というディレクトリとファイルをそれぞれ作成します。
「 /var 」ディレクトリに移動して、以下のコマンドを実行し、ディレクトリとファイルを作成します。
sudo mkdir -p www/html
sudo vi index.html
作成した「 index.html 」を以下のように変更します。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>テストページ</title>
</head>
<body>
<p>テストページ</p>
</body>
</html>
「 /var/www/html 」の所有者を変更します。以下のコマンドを実行します。
sudo chown -R nginx:nginx /var/www/html
ファイルとディレクトリのパーミッションを以下のように変更します。
sudo chmod -R 755 /var/www/html
SELinux を有効にしている場合、以下のように変更します。
sudo chcon -R -t httpd_sys_content_t /var/www/html
Nginx を再起動します。
sudo systemctl restart nginx
問題が発生する場合は、エラーログを確認します。
sudo tail -f /var/log/nginx/error.log
リバースプロキシとして利用する
リバースプロキシとしての設定を「 exproxy.conf 」に設定を行います。
/etc/nginx/conf.d/exproxy.conf
server {
server_name example.com;
location / {
proxy_pass http://localhost:3000/;
}
}
バージョン情報を隠す
設定ファイルに以下を追加します。
http {
server_tokens off;
~
Options -Indexes
デフォルトでインデックスページは非表示になっているため明示的な指定は不要です。
autoindex off;