質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
nginx

nginixは軽量で高性能なwebサーバーの1つです。BSD-likeライセンスのもとリリースされており、あわせてHTTPサーバ、リバースプロキシ、メールプロキシの機能も備えています。MacOSX、Windows、Linux、上で動作します。

Q&A

解決済

1回答

6285閲覧

nginx virtualhostの設定が効かない

y214

総合スコア33

nginx

nginixは軽量で高性能なwebサーバーの1つです。BSD-likeライセンスのもとリリースされており、あわせてHTTPサーバ、リバースプロキシ、メールプロキシの機能も備えています。MacOSX、Windows、Linux、上で動作します。

0グッド

0クリップ

投稿2015/10/07 11:34

概要

nginxのvirtualhostの設定がうまく効きません。
以下のような二つのvirtualhost

  • test.com
  • example.com

を立てたいのですが、いざブラウザからアクセスするとうまくアクセスできません。

各virtualhostで設定したいことは

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

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

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 コマンドを利用するといいと思います。

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/

投稿2015/10/07 16:05

TaichiYanagiya

総合スコア12146

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問