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

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

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

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

Q&A

解決済

1回答

472閲覧

www有りURLからwww無しURLへリダイレクト後、再度www無しURLを開くと「この接続ではプライバシーが保護されません」が表示される

AWS_NGINX

総合スコア18

nginx

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

2グッド

2クリップ

投稿2019/01/23 17:49

EC2上にnginxサーバを構築中です。

wwwなしURL及びhttpsでのURL正規化したサイトを作成する予定の為、
http://www.hoge.com及びhttps://www.hoge.com、http://hoge.comにアクセスがあった場合に
https://hoge.comへリダイレクトさせようとしています。

http://hoge.com→https://hoge.comへのリダイレクトは想定通り動作しているようです。

www有りについてもchromeのシークレットウィンドウにて、初回は正しくリダイレクトするのですが、
リダイレクト後に再度www有りのhttpまたはhttpsアドレスをアドレス欄に直接入力すると
「この接続ではプライバシーが保護されません」が表示されます。
一度でもhttpsのwww無しURLに接続すると、その後www有りURL接続時に当該エラーが発生する模様です。

尚、当然ながらブラウザでアクセス前にエラーを出しているのでnginxのアクセスログ、エラーログは
出力されていません。

リダイレクト設定箇所

html

1#httpアクセスをhttpsへリダイレクト 2 server { 3 listen 80; 4 listen [::]:80; 5 server_name hoge.com www.hoge.com; 6 rewrite ^(.*)$ https://hoge.com$request_uri permanent; 7# rewrite ^ https://hoge.com$request_uri?; 8 } 9#https wwwへのアクセスをwwwなしにリダイレクト 10 server { 11 listen 443 ssl; 12 listen [::]:443 ssl; 13 server_name www.hoge.com; 14 rewrite ^(.*)$ https://hoge.com$request_uri permanent; 15# rewrite ^ https://hoge.com$request_uri?; 16 } 17 18 server { 19 listen 443 ssl http2 default_server; 20 listen [::]:443 ssl http2 default_server; 21 server_name hoge.com; 22~~~~~~

試したこと

以下の方法によるリダイレクト
return 302 http...
rewrite ^ http...

補足情報(FW/ツールのバージョンなど)

Amazon Linux2
nginx/1.12.2
wwwについてはRoute53にてCNameを設定しています。

nginx.conf全文

html

1user nginx; 2worker_processes auto; 3worker_rlimit_nofile 100000; 4 5pid /run/nginx.pid; 6 7# Load dynamic modules. See /usr/share/nginx/README.dynamic. 8include /usr/share/nginx/modules/*.conf; 9 10events { 11 worker_connections 2048; 12 multi_accept on; 13 use epoll; 14} 15 16http { 17 server_tokens off; 18 log_format main '$remote_addr - $remote_user [$time_local] "$request" ' 19 '$status $body_bytes_sent "$http_referer" ' 20 '"$http_user_agent" "$http_x_forwarded_for"'; 21 22 charset UTF-8; 23 default_type text/html; 24 sendfile on; 25 tcp_nopush on; 26 tcp_nodelay on; 27 keepalive_timeout 10; 28 client_header_timeout 10; 29 client_body_timeout 10; 30 reset_timedout_connection on; 31 send_timeout 10; 32 gzip on; 33 gzip_http_version 1.0; 34 gzip_disable "msie6"; 35 gzip_proxied any; 36 gzip_min_length 1024; 37 gzip_comp_level 6; 38 gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rs 39s text/javascript application/javascript; 40 open_file_cache max=100000 inactive=20s; 41 open_file_cache_valid 30s; 42 open_file_cache_min_uses 2; 43 open_file_cache_errors on; 44 types_hash_max_size 2048; 45 46 include /etc/nginx/mime.types; 47 48 # Load modular configuration files from the /etc/nginx/conf.d directory. 49 # See http://nginx.org/en/docs/ngx_core_module.html#include 50 # for more information. 51 include /etc/nginx/conf.d/*.conf; 52 53#httpアクセスをhttpsへリダイレクト 54 server { 55 listen 80; 56 listen [::]:80; 57 server_name hoge.com www.hoge.com; 58 rewrite ^(.*)$ https://hoge.com$request_uri permanent; 59# rewrite ^ https://hoge.com$request_uri?; 60 } 61#https wwwへのアクセスをwwwなしにリダイレクト 62 server { 63 listen 443 ssl; 64 listen [::]:443 ssl; 65 server_name www.hoge.com; 66 rewrite ^(.*)$ https://hoge.com$request_uri permanent; 67# rewrite ^ https://hoge.com$request_uri?; 68 } 69 70 server { 71 listen 443 ssl http2 default_server; 72 listen [::]:443 ssl http2 default_server; 73 server_name hoge.com; 74 75 root /usr/share/nginx/html; 76 index index.php index.html index.htm; 77 access_log /var/log/nginx/access.log; 78 error_log /var/log/nginx/error.log; 79 ssl_protocols TLSv1.2 TLSv1.1 TLSv1; 80 ssl_ciphers EECDH+AESGCM+AES128:EECDH+AESGCM:EECDH+AES128:EECDH+AES256; 81 ssl_prefer_server_ciphers on; 82 ssl_dhparam /etc/ssl/certs/nginx/dhparam.pem; 83 ssl_certificate /etc/letsencrypt/live/hoge.com/fullchain.pem; 84 ssl_certificate_key /etc/letsencrypt/live/hoge.com/privkey.pem; 85 86 # Load configuration files for the default server block. 87 include /etc/nginx/default.d/*.conf; 88 89 location ~ .php { 90 fastcgi_pass 127.0.0.1:9000; 91 fastcgi_index index.php; 92 fastcgi_split_path_info ^(.+.php)(.*)$; 93 fastcgi_param PATH_INFO $fastcgi_path_info; 94 fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info; 95 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 96 fastcgi_param DATABASE_URL mysql://xxxxxxxx; 97 include fastcgi_params; 98 } 99 100 location / { 101 try_files $uri $uri/ /index.php?u=$uri&$args; 102 } 103 } 104}

以上、宜しくおねがいします。

ozwind918, set0gut1👍を押しています

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

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

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

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

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

guest

回答1

0

自己解決

リダイレクト要求前のブラウザ挙動の問題なので、nginxは無関係な予感がして調べたところ、
nginxではなくSSL証明書の問題でした。

letsencryptを使用していたので次のコマンドで、SSL証明書にwww.hoge.comを追加する事で、想定通り動作しました。
sudo /usr/local/bin/certbot-auto --expand -d hoge.com -d www.hoge.com

投稿2019/01/23 18:32

AWS_NGINX

総合スコア18

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問