nginx virtualhostの設定が効かない
解決済
回答 1
投稿
- 評価
- クリップ 0
- VIEW 4,098
概要
nginxのvirtualhostの設定がうまく効きません。以下のような二つのvirtualhost
- test.com
- example.com
各virtualhostで設定したいことは
test.com
- http, https での通信
- http://test.com を https://test.com にリダイレクト
example.com
- http での通信
です。
test.comは問題なく動作するのですが、example.comにアクセスするとhttpsへリダイレクトがかかり、アクセスエラーとなります。
下記に設定ファイル記載します。
至らない部分のご教授お願い致します。
開発環境
- vagrant(CentOS 6.5)
- nginx 1.8.0
設定
defautl.conf# - - - - - - - - - - - - - - - - - -
# default.conf
# ■ test.com
# - http, https での通信
# - http://test.com を https:://test.com にリダイレクト
# ■ example.com
# - http での通信
# - fast cgi (socket)
# - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - -
# test.com
# - - - - - - - - - - - - - - - - - -
server {
listen 80;
server_name .test.com;
return 301 https://test.com$request_uri;
}
server {
listen 443;
server_name .test.com;
ssl on;
ssl_certificate /etc/nginx/conf.d/ssl/server.crt;
ssl_certificate_key /etc/nginx/conf.d/ssl/server.key;
if ($http_host = www.test.com) {
rewrite (.*) http://test.com$1;
}
charset UTF-8;
access_log /var/log/nginx/test.access.log main;
error_log /var/log/nginx/test.error.log warn;
location / {
root /var/env/test/www;
index index.html index.php;
auth_basic "Restricted";
auth_basic_user_file /etc/nginx/.htpasswd;
}
error_page 404 /404.html;
location = /404.html {
root /var/env/test/www;
}
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /var/env/test/www;
}
try_files $uri $uri/ /index.php?q=$uri&$args;
location ~ \.php$ {
root /var/env/test/www;
try_files $uri = 404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
# - - - - - - - - - - - - - - - - - -
# example.com
# - - - - - - - - - - - - - - - - - -
server {
listen 80;
server_name .example.com;
if ($http_host = www.example.com) {
rewrite (.*) http://example.com$1;
}
charset UTF-8;
access_log /var/log/nginx/example.access.log main;
error_log /var/log/nginx/example.error.log warn;
location / {
root /var/env/example/www;
index index.html index.php;
auth_basic "Restricted";
auth_basic_user_file /etc/nginx/.htpasswd;
}
error_page 404 /404.html;
location = /404.html {
root /var/env/example/www;
}
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /var/env/example/www;
}
try_files $uri $uri/ /index.php?q=$uri&$args;
location ~ \.php$ {
root /var/env/example/www;
try_files $uri = 404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
ローカルPCのhosts
192.168.30.10 test.com
192.168.30.10 example.com
-
気になる質問をクリップする
クリップした質問は、後からいつでもマイページで確認できます。
またクリップした質問に回答があった際、通知やメールを受け取ることができます。
クリップを取り消します
-
良い質問の評価を上げる
以下のような質問は評価を上げましょう
- 質問内容が明確
- 自分も答えを知りたい
- 質問者以外のユーザにも役立つ
評価が高い質問は、TOPページの「注目」タブのフィードに表示されやすくなります。
質問の評価を上げたことを取り消します
-
評価を下げられる数の上限に達しました
評価を下げることができません
- 1日5回まで評価を下げられます
- 1日に1ユーザに対して2回まで評価を下げられます
質問の評価を下げる
teratailでは下記のような質問を「具体的に困っていることがない質問」、「サイトポリシーに違反する質問」と定義し、推奨していません。
- プログラミングに関係のない質問
- やってほしいことだけを記載した丸投げの質問
- 問題・課題が含まれていない質問
- 意図的に内容が抹消された質問
- 過去に投稿した質問と同じ内容の質問
- 広告と受け取られるような投稿
評価が下がると、TOPページの「アクティブ」「注目」タブのフィードに表示されにくくなります。
質問の評価を下げたことを取り消します
この機能は開放されていません
評価を下げる条件を満たしてません
質問の評価を下げる機能の利用条件
この機能を利用するためには、以下の事項を行う必要があります。
- 質問回答など一定の行動
-
メールアドレスの認証
メールアドレスの認証
-
質問評価に関するヘルプページの閲覧
質問評価に関するヘルプページの閲覧
checkベストアンサー
0
"http://example.com/" にアクセスすると、server {server_name .example.com;} 箇所には該当せずに、server {server_name .test.com;} 箇所で処理され https://test.com/ にリダイレクトされるということでしょうか。
nginx の設定は問題ないように思います。
各 server ごとに access_log, error_log を分けているので、"http://example.com/" でアクセスしたときに、意図した /var/log/nginx/example.access.log にログが出力されるかどうか調査するといいと思います。
また、ブラウザではなく、サーバーのシェルから試すのであれば、curl コマンドを利用するといいと思います。
nginx の設定は問題ないように思います。
各 server ごとに access_log, error_log を分けているので、"http://example.com/" でアクセスしたときに、意図した /var/log/nginx/example.access.log にログが出力されるかどうか調査するといいと思います。
また、ブラウザではなく、サーバーのシェルから試すのであれば、curl コマンドを利用するといいと思います。
curl -k -D - -H "Host: test.com" -u username:password http://127.0.0.1/
curl -k -D - -H "Host: test.com" -u username:password https://127.0.0.1/
curl -k -D - -H "Host: example.com" -u username:password http://127.0.0.1/
投稿
-
回答の評価を上げる
以下のような回答は評価を上げましょう
- 正しい回答
- わかりやすい回答
- ためになる回答
評価が高い回答ほどページの上位に表示されます。
-
回答の評価を下げる
下記のような回答は推奨されていません。
- 間違っている回答
- 質問の回答になっていない投稿
- スパムや攻撃的な表現を用いた投稿
評価を下げる際はその理由を明確に伝え、適切な回答に修正してもらいましょう。
15分調べてもわからないことは、teratailで質問しよう!
- ただいまの回答率 88.10%
- 質問をまとめることで、思考を整理して素早く解決
- テンプレート機能で、簡単に質問をまとめられる