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

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

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

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

Q&A

解決済

1回答

934閲覧

KUSANAGI+nginxでwwwなしをwwwありのURLにリダクレクトさせる設定は?

munemune

総合スコア13

nginx

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

0グッド

0クリップ

投稿2017/12/26 01:42

AWS+KUSANGI+nginxでワードプレスのサイトを運営しています。

例えば、
https://www.hogehoge.jpというURLに、
http://www.hogehoge.jpからは自動的にリダイレクトするようkusanagi上の設定ではできていますが、wwwなしの
https://hogehoge.jp
http://hogehoge.jp
のURLはまだ自動的にリダイレクトされるように設定できていません。

kusanagi_html_http.confは現状以下のようになっています。
どのようにしたら、wwwなしのこの2つのURLをwwwありにリダイレクトさせることができるか、教えてください。
あと、他の設定ファイル(kusanagi_html_ssl.confとか?)も何か設定しなければいけない場合も合わせてご教授ください。

#======================================= # www.hogehoge.jp #--------------------------------------- server { listen 80; server_name www.hogehoge.jp; access_log /home/kusanagi/kusanagi_html/log/nginx/access.log main; error_log /home/kusanagi/kusanagi_html/log/nginx/error.log warn; rewrite ^(.*)$ https://www.hogehoge.jp$request_uri permanent; # SSL ONLY charset UTF-8; client_max_body_size 16M; root /home/kusanagi/kusanagi_html/DocumentRoot; index index.php index.html index.htm; rewrite /wp-admin$ $scheme://$host$uri/ permanent; location / { try_files $uri $uri/ /index.php?$args; } location = /favicon.ico { log_not_found off; access_log off; } location ~* /.well-known { allow all; } location ~* /. { deny all; } #include templates.d/multisite.conf; location ~* /(?:uploads|files)/.*.php$ { deny all; } location ~* .(jpg|jpeg|gif|png|css|js|swf|ico|pdf|svg|eot|ttf|woff)$ { expires 60d; access_log off; } location ~* /wp-login.php|/wp-admin/((?!admin-ajax.php).)*$ { satisfy any; allow 0.0.0.0/0; allow 127.0.0.1; deny all; auth_basic "basic authentication"; auth_basic_user_file "/home/kusanagi/.htpasswd"; location ~ [^/].php(/|$) { fastcgi_split_path_info ^(.+?.php)(/.*)$; if (!-f $document_root$fastcgi_script_name) { return 404; } fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; fastcgi_buffers 256 128k; fastcgi_buffer_size 128k; fastcgi_intercept_errors on; fastcgi_read_timeout 120s; } } location ~ [^/].php(/|$) { fastcgi_split_path_info ^(.+?.php)(/.*)$; if (!-f $document_root$fastcgi_script_name) { return 404; } fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; fastcgi_buffers 256 128k; fastcgi_buffer_size 128k; fastcgi_intercept_errors on; fastcgi_read_timeout 120s; set $do_not_cache 1; ## page cache set $device "pc"; if ($request_method = POST) { set $do_not_cache 1; } if ($query_string != "") { set $do_not_cache 1; } if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in") { set $do_not_cache 1; } if ($request_uri ~* "(/wp-admin/|/xmlrpc.php|/wp-(app|cron|login|register|mail).php|wp-.*.php|/feed/|index.php|wp-comments-popup.php|wp-links-opml.php|wp-locations.php|sitemap(_index)?.xml|[a-z0-9_-]+-sitemap([0-9]+)?.xml)") { set $do_not_cache 1; } if ($http_user_agent ~* " Android |(iPad|Android; Tablet; .+Firefox") { set $device "tablet"; } if ($http_user_agent ~* " Android .+ Mobile |(iPhone|(iPod|IEMobile|Android; Mobile; .+Firefox|Windows Phone") { set $device "smart"; } fastcgi_cache wpcache; fastcgi_cache_key "$device:$request_method:$scheme://$host$request_uri"; fastcgi_cache_valid 200 10m; fastcgi_no_cache $do_not_cache; fastcgi_cache_bypass $do_not_cache; add_header X-F-Cache $upstream_cache_status; add_header X-Signature KUSANAGI; } }

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

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

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

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

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

TaichiYanagiya

2017/12/26 08:56

server_name に (www なしの) hogehoge.jp がありませんが、別の .conf で設定していますか?
munemune

2017/12/26 08:57

まだ何も設定していません。(別の.confでも設定していません)
guest

回答1

0

ベストアンサー

server_name hogehoge.jp; が他に設定されていないのであれば、今の設定で http://hogehoge.jphttps://www.hogehoge.jp にリダイレクトされると思うのですが、なりませんでしょうか?

試しに、server_namewww.hogehoge.jp, hogehoge.jp 2つとも設定して、nginx -t で競合がないか確認ください。

server { listen 80; server_name www.hogehoge.jp hogehoge.jp; (略) rewrite ^(.*)$ https://www.hogehoge.jp$request_uri permanent; # SSL ONLY (略) }

また、https://hogehoge.jphttps://www.hogehoge.jp の書き換えは、listen 443server {} を新たに設定すればいいと思います。
設定ファイルは、kusanagi_html_ssl.conf に追記してもいいですし、新たに .conf ファイルを作ってもいいです。

server { listen 443; server_name hogehoge.jp; (略) rewrite ^(.*)$ https://www.hogehoge.jp$request_uri permanent; # SSL ONLY }

投稿2017/12/26 09:19

TaichiYanagiya

総合スコア12146

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

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

munemune

2017/12/26 12:13

ありがとうございました!アドバイス通りに設定して無事解決しました!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問