각종 팁&정보를 나누는 곳입니다.
누군가에게 도움이 된다 싶으시면 언제라도 포스팅 :)
글 등록하기 | 내글 관리하기 | 연재글 | 보관함
nginx https redirect www to non-www
첨부파일 https://imweb.eond.com/webtip/398106
April 6, 2017 8.8kviews NGINX CENTOS


Hi i was config my nginx block to redirect all www to non-www, this is my config:


server {    listen       80;    server_name  mysite.com www.mysite.com;    return       301 https://mysite.com$request_uri; }
 server {    listen       443 ssl http2;    server_name  mysite.com;    root   /usr/share/nginx/html;     ssl on;    ssl_certificate /ssl/ssl-bundle.crt;    ssl_certificate_key /ssl/mysite.key;    ssl_session_cache shared:SSL:20m;    ssl_session_timeout 10m;    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;    ssl_prefer_server_ciphers on;    ssl_ciphers ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:!DSS;    ssl_buffer_size 8k;    ssl_session_tickets off;     ssl_stapling on;    ssl_stapling_verify on;    ssl_trusted_certificate /ssl/ssl-trusted.crt;    resolver 8.8.8.8 8.8.4.4 valid=300s;    resolver_timeout 10s;     #add_header X-Content-Type-Options "nosniff";     location / {        index index.php index.html index.htm;    try_files $uri $uri/ /index.php?$uri&$args;    }     location /internal_data/ {        internal;        allow 127.0.0.1;        deny all;        }         location /library/ {        internal;        allow 127.0.0.1;        deny all;        }     location ~ \.php$ {    try_files $uri =404;    fastcgi_pass 127.0.0.1:9000;    fastcgi_index index.php;    fastcgi_param SCRIPT_FILENAME  $document_root$fastcgi_script_name;    include fastcgi_params;    }     location ~* \.(?:ico|css|gif|jpe?g|js|png|svg|svgz|swf)(\?.+)?$ {        access_log              off;        log_not_found           off;        expires                 1y;    } 
location ~ \.(jpe?g|png|gif)$ {     valid_referers none blocked mysite.com *.mysite.com;     if ($invalid_referer) {        return   403;        }    } }


working fine just for non-www to https but not work from www to non-www,
this is result from curl -I http://www.mysite.com :

curl: (6) Couldn't resolve host 'www.mysite.com'

is there any iam miss?
thank you.


기존에 맨 위에 주석 처리된 부분으로 작업했었으나,

https://www.eond.com 에 대한 return이 적용이 되지 않아, 위 팁을 적용하여

주석줄 아래 코드로 변경해주었습니다.

# server {
#     listen       80;
#     server_name  eond.com www.eond.com;
#     root /home/eond/www;
#     location / {
#         return 301 https://eond.com$request_uri;
#     }
# }
server {
    listen       80;
    server_name  eond.com www.eond.com;
    return       301 https://eond.com$request_uri;
}


server {
   listen 443 ssl http2;
   server_name eond.com;
   root /home/eond/www;
   index index.php index.html index.htm;
   charset utf-8;

   # if ($http_host = "www.eond.com") {
   #  rewrite ^ https://eond.com$request_uri permanent;
   # }

   location / {
      try_files $uri $uri/ =404;
   }

   include snippets/well-known.conf;
   #include snippets/wp-rewrite.conf;
   include snippets/xe-rewrite.conf;

   location ~ \.php$ {
      fastcgi_pass unix:/run/php/pool.eond.sock;
      include snippets/fastcgi-php.conf;
      limit_req zone=antiddos burst=12 nodelay;
   }

   location ~* \.(jpe?g|png|gif|bmp|ico|svg|swf|flv|avi|wav|mp[34]|woff|ttf|s?css|less|js)$ {
      expires 15d;
      log_not_found off;
   }

   ssl_certificate /etc/letsencrypt/live/eond.com/fullchain.pem;
       ssl_certificate_key /etc/letsencrypt/live/eond.com/privkey.pem;
       ssl_trusted_certificate /etc/letsencrypt/live/eond.com/chain.pem;
       ssl_dhparam /etc/nginx/ssl/dhparam.pem;
       ssl_stapling on;
       ssl_stapling_verify on;
}

# server {
#  listen 80;
#  server_name www.eond.com;
#
#  location / {
#     return 301 https://eond.com$request_uri;
#     expires epoch;
#  }
#
#  include snippets/well-known.conf;
# }

server {
   listen 80;
   listen 443 ssl http2;
    server_name www.eond.com;
    return 301 $scheme://eond.com$request_uri;
}


코멘트 0
접기/펴기 | 댓글 새로고침
 
 
Total 683 articles in 57 / 57 pages
번호 제목 제목 조회 수 날짜날짜
11 [GitHub] git branch 변경 94 2022/01/21
10 [GitHub] 다른 깃헙 가져오기 cli 106 2022/01/23
9 [웹 제작] 임시이미지 사이트 https://placeimg.com/ 66 2022/11/27
8 [웹 제작] website to learn 파일 44 2022/12/27
7 [사이트관리] 사이트맵 FAQ 50 2023/01/31
6 [윈도우] 갑자기 윈도우10 알트탭(alt + tab)이 정상 사용 안될 때 확인 및 처리 방법? : 네이버 포스트 (naver.com) 124 2023/03/17
5 [웹 제작] 홈페이지를 만드는 방법에는 어떤 게 있나요? 93 2023/04/06
4 [기타] WidgetBot - 디스코드 채팅 방을 블로그에 달자! 파일 106 2023/05/04
3 [메신저] 카카오톡 알림톡 개발하기 #1 109 2023/08/01
2 카카오지도 가려지는 문제 59 2024/02/01
1 [아임웹] 아임웹 - 모바일 메뉴 하단에 SNS 아이콘 추가하는 코드 224 2024/03/15

해시태그 디렉터리

지금 이순간

오늘의 핫게시물