LoginSignup
13
13

More than 3 years have passed since last update.

Nginx で VirtualHost を使う

Last updated at Posted at 2020-09-12

Nginx で VirtualHost を使う方法です。

次の例では、ekzemplaro.org と corona.ekzemplaro.org を受け付ける設定です。
どちらも、Let's Encrypt を使い、http は https にリダイレクトされます。

/etc/nginx/nginx.conf
#
worker_processes  1;

error_log  /var/log/nginx/error.log;

events {
    worker_connections  1024;
}


http {
    include      mime.types;
    default_type  application/octet-stream;

    sendfile  on;

    keepalive_timeout  65;
    types_hash_max_size 4096;
    server_names_hash_bucket_size 128;

    server {
        listen       80;
        server_name   ekzemplaro.org;
        access_log  /var/log/nginx/access.log;

        return 301 https://$host$request_uri;
        }

    server {
        listen  443 ssl;
        server_name   ekzemplaro.org;

        location / {
            root    /var/www/html;
            index  index.html index.htm;
        }



ssl_certificate     /etc/letsencrypt/live/ekzemplaro.org/fullchain.pem;
ssl_certificate_key   /etc/letsencrypt/live/ekzemplaro.org/privkey.pem;

     }

server {
    listen  80;
    server_name corona.ekzemplaro.org;
    access_log  /var/log/nginx/corona.access.log;

    return 301 https://$host$request_uri;

    }

server {
    listen  443 ssl;
    server_name   corona.ekzemplaro.org;

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

    root /var/www/html/corona;

ssl_certificate     /etc/letsencrypt/live/corona.ekzemplaro.org/fullchain.pem;
ssl_certificate_key   /etc/letsencrypt/live/corona.ekzemplaro.org/privkey.pem;

    }


}

設定ファイルの確認

$ sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

サーバーの再起動

sudo systemctl restart nginx

関連ページ
Apache2 で VirtualHost を使う

13
13
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13
13