1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Ubuntu18 * NginxでVirtualHostの設定

Posted at

UbuntuにNginxを入れてVirtualHostを設定する

備忘録も兼ねて
(さくらのクラウドUbuntu18.04で確認済み)

1. Nginxインストール

パッケージマネージャ経由でインストール。事前にapt update/upgradeはやっておく。

$ sudo apt update
$ sudo apt upgrade
$ sudo apt install nginx

2. Nginxの設定ファイルを開く

$ sudo nano /etc/nginx/sites-available/default

3. VirtualHostのserver設定を追記する

server {
        listen 80;

        server_name www.example.com;
        root [ドキュメントルートのパス];
}

ハマりどころとして、デフォルトで入っている設定に

server {
        listen 80 default_server;
        listen [::]:80 default_server;

        root /var/www/html;
        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;
        }
}

とVirtualHost設定の肝である server_name にアンダースコアが設定されており、これが入っていると全てのserver_nameとマッチさせることになるので用がなければ外しておくのが良い。

4. configtestと再起動

Nginxのconfigをテストする

$ sudo /etc/init.d/nginx configtest
 * Testing nginx configuration                                           [ OK ]

OKならnginx restartして、ドキュメントルートにindex.html置いてサイトを確認。

1
0
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?