概要
nginxを利用する際に得する小技集
BASIC認証
vi /etc/nginx/conf/nginx.conf
server {
listen 80;
server_name portal.intra.exam.local;
access_log /etc/nginx/logs/portal.intra.exam.local main;
location / {
auth_basic "AuthPage"; # 認証時に表示されるメッセージ
auth_basic_user_file /etc/nginx/conf/.htpasswd; # .htpasswdファイルのパス
root /docs/portal.intra.exam.local;
index index.html index.htm;
}
}
.htpasswdファイルは、Apacheのhtpasswdが必要になってしまうため、
http://www.luft.co.jp/cgi/htpasswd.php
のようなページの力を借りて、BASIC認証のID/PWを取得し、.htpasswdファイルに追加することで対応が可能です。
バーチャルホスト設定
vi /etc/nginx/conf/nginx.conf
server {
listen 80;
server_name portal.intra.exam.local;
access_log /etc/nginx/logs/portal.intra.exam.local main;
location / {
root /docs/portal.intra.exam.local;
index index.html index.htm;
}
}
server {
listen 80;
server_name mail.intra.exam.local;
access_log /etc/nginx/logs/mail.intra.exam.local main;
location / {
root /docs/mail.intra.exam.local;
index index.html index.htm;
}
}
バージョン情報隠蔽
http {
server_tokens off;
...
}
SSLに対応する
server {
server_name secure.intra.exam.local;
listen 443 default ssl;
ssl on;
# サーバ証明書(サーバ証明書に中間CA証明書を連結したもの)
ssl_certificate /etc/nginx/conf/cert.pem;
# 秘密鍵
ssl_certificate_key /etc/nginx/conf/cert.key;
}
KeyとCSRを発行する
Keyを作成する
/usr/local/ssl/bin/openssl req -new -key /etc/nginx/conf/server.2016.key > /etc/nginx/conf/server.2016.csr
CSRを作成する
bash
/usr/local/ssl/bin/openssl req -new -key /etc/nginx/conf/server.2016.key > /etc/nginx/conf/server.2016.csr
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [AU]:JP #日本は"JP"と入力
State or Province Name (full name) [Some-State]:Osaka #都道府県を入力
Locality Name (eg, city) []:Osaka #市町村を入力
Organization Name (eg, company) [Internet Widgits Pty Ltd]:Exam Company #会社名を入力
Organizational Unit Name (eg, section) []:ICT Team #部署名を入力
Common Name (e.g. server FQDN or YOUR name) []:matt.exam.labo #アクセスされるFQDNを入力(ここが最も重要!)
Email Address []: #未入力でよい
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []: #未入力でよい
An optional company name []: #未入力でよい
この後、できあたがったCSRをもとに認証局でもらったCerファイルをnginxから参照させればOK
ReverseProxy
server {
listen 80;
server_name nginx.exam.labo;
location / {
proxy_pass http://portal.example.com/;
}
location /test {
proxy_pass https://www.example.com/;
}
}