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

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

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

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

リダイレクト

プログラムの入力元や出力先を通常とは別の場所に転送させることをリダイレクトと呼びます。

Q&A

解決済

1回答

3831閲覧

NginxでのURIの書き換えについての質問です。

marshmallowy

総合スコア204

nginx

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

リダイレクト

プログラムの入力元や出力先を通常とは別の場所に転送させることをリダイレクトと呼びます。

0グッド

0クリップ

投稿2016/02/22 01:31

NginxでのURIの書き換えについての質問です。

ご教授の程、よろしくお願いします。

###実現したいこと
[No.1] http://example.vrtにアクセスした時、https://www.example.vrt/ にリダイレクトさせたいです。

[No.2] https://example.vrtにアクセスした時、https://www.example.vrt/ にリダイレクトさせたいです。

[No.3] http://www.example.vrt/index.php にアクセスした時、https://www.example.vrt/ にリダイレクトさせたいです。

[No.4] http://example.vrt/index.php にアクセスした時、https://www.example.vrt/ にリダイレクトさせたいです。

###発生している事象
[No.1] http://example.com にアクセスした時、https://www.example.com/ にリダイレクトができない 。

[No.2] https://example.com にアクセスした時、https://www.example.com/ にリダイレクトができない 。

[No.3] http://www.example.com/index.php にアクセスした時、https://www.example.com/ にリダイレクトができない 。

[No.4] http://example.com/index.php にアクセスした時、https://www.example.com/ にリダイレクトができない 。

###設定ファイル

server { listen 80; server_name www.example.vrt; return 301 https://www.example.vrt; } server { listen 443 ssl; server_name www.example.vrt; ssl_certificate /usr/local/nginx/ssl/server.crt; ssl_certificate_key /usr/local/nginx/ssl/server.key; root /var/www/html/example; access_log /var/log/nginx/ssl_example_access.log; error_log /var/log/nginx/ssl_example_error.log notice; rewrite_log on; location / { index index.html index.php; try_files $uri $uri/ @handler; expires max; } location /. { return 404; } location @handler { rewrite / /index.php; } location = / { set $first_language $http_accept_language; set $language_suffix 'english'; if ($first_language ~* 'ja') { set $language_suffix 'japanese'; } return $scheme://$host/$language_suffix/$1; } location ~ .php/ { rewrite ^(.*.php)/ $1 last; } location ~ .php$ { if (!-e $request_filename) { rewrite / /index.php last; } expires off; fastcgi_pass unix:/var/run/php5-fpm.sock; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include /usr/local/nginx/fastcgi_params; fastcgi_buffer_size 128k; fastcgi_buffers 256 4k; fastcgi_busy_buffers_size 256k; fastcgi_temp_file_write_size 256k; } }

###補足情報
Vagrant Installed Version: 1.8.0

PHP 5.5.9-1ubuntu4.14 (cli) (built: Oct 28 2015 01:34:46)
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2014 Zend Technologies
with Zend OPcache v7.0.3, Copyright (c) 1999-2014, by Zend Technologies

PHP 5.5.9-1ubuntu4.14 (fpm-fcgi) (built: Oct 28 2015 01:38:24)
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2014 Zend Technologies
with Zend OPcache v7.0.3, Copyright (c) 1999-2014, by Zend Technologies

nginx version: nginx/1.9.11
built by gcc 4.8.4 (Ubuntu 4.8.4-2ubuntu1~14.04.1)
built with OpenSSL 1.0.1f 6 Jan 2014
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --sbin-path=/usr/sbin/nginx --conf-path=/usr/local/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module --with-http_auth_request_module --with-threads --with-stream --with-stream_ssl_module --with-http_slice_module --with-mail --with-mail_ssl_module --with-file-aio --with-http_v2_module --add-dynamic-module=/usr/build/3party_module/nginx_accept_language_module

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

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

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

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

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

guest

回答1

0

ベストアンサー

「example.com」と「example.vrt」が混在していますが、「example.vrt」に統一して解釈します。

既存の設定で No.3 は出来ていると思うのですが、どのように確認ましたか?
No.1, 4 は同じことですよね?

下記のように www あり/なしのホスト名ごとに server を設定すればいいと思います。

server { listen 80; server_name example.vrt www.example.vrt; # No.1, 3, 4 のホスト名 return 301 https://www.example.vrt; } server { listen 443 ssl; server_name example.vrt; # No.2 のホスト名 ssl_certificate /usr/local/nginx/ssl/example.vrt.crt; # 証明書(CN=example.vrt) ssl_certificate_key /usr/local/nginx/ssl/example.vrt.key; return 301 https://www.example.vrt/; } server { listen 443 ssl; server_name www.example.vrt; (略) }

投稿2016/02/24 06:44

TaichiYanagiya

総合スコア12146

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

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

marshmallowy

2016/02/24 07:23

ご教授ありがとう御座います。 リダイレクトを対応することができました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問