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}
以上、宜しくおねがいします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。