Nginx - Ubuntu

  • 作成日:
  • 最終更新日:2025/06/25

Nginx のインストール

nginx をインストールするには、以下のようにします。

sudo apt install nginx

サービスの起動や状態の確認については、以下のようにします。

// サービスの起動
sudo systemctl start nginx

// 自動起動
sudo systemctl enable nginx

// 状態の確認
sudo systemctl status nginx

設定ファイル

nginx の設定ファイルの場所は、「 /etc/nginx 」にあり、「 nginx.conf 」が設定ファイルです。

/etc/nginx/nginx.conf

user www-data;
worker_processes auto;
worker_cpu_affinity auto;
pid /run/nginx.pid;
error_log /var/log/nginx/error.log;
include /etc/nginx/modules-enabled/*.conf;

events {
        worker_connections 768;
        # multi_accept on;
}

http {

        ##
        # Basic Settings
        ##

        sendfile on;
        tcp_nopush on;
        types_hash_max_size 2048;
        server_tokens build; # Recommended practice is to turn this off

        # server_names_hash_bucket_size 64;
        # server_name_in_redirect off;

        include /etc/nginx/mime.types;
        default_type application/octet-stream;

        ##
        # SSL Settings
        ##

        ssl_protocols TLSv1.2 TLSv1.3; # Dropping SSLv3 (POODLE), TLS 1.0, 1.1
        ssl_prefer_server_ciphers off; # Don't force server cipher order.

        ##
        # Logging Settings
        ##

        access_log /var/log/nginx/access.log;

        ##
        # Gzip Settings
        ##

        gzip on;

        # gzip_vary on;
        # gzip_proxied any;
        # gzip_comp_level 6;
        # gzip_buffers 16 8k;
        # gzip_http_version 1.1;
        # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

        ##
        # Virtual Host Configs
        ##

        include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/*;
}


#mail {
#       # See sample authentication script at:
#       # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
#
#       # auth_http localhost/auth.php;
#       # pop3_capabilities "TOP" "USER";
#       # imap_capabilities "IMAP4rev1" "UIDPLUS";
#
#       server {
#               listen     localhost:110;
#               protocol   pop3;
#               proxy      on;
#       }
#

プロキシサーバーの設定

/etc/nginx/conf.d/exproxy.confにプロキシサーバーの設定ファイルを作成します。

/etc/nginx/conf.d/exproxy.conf

server {
    listen 80;
    server_name example.com; # ここに自分が利用しているドメイン名を指定

    location / {
        proxy_pass http://localhost:4001;
        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;
    }
}

設定ファイルを編集後は、nginx を再起動します。

sudo systemctl restart nginx

Express.js のアプリをnpm startで起動してページが表示されれば設定は完了しています。