2017-01-18 3 views
0

첫 번째 서버 블록을 추가하여 www.example.com을 https://example.com으로 리디렉션하려고합니다. 하지만 리디렉션되지 않습니다. SEO 문제와 사용자에 대한 보안 경고를 피하려면 영구적으로 리디렉션되어야합니다. 서버 블록은 443 포트와 인증서 정의에 https를 활성화 ssl 키워드를 누락 최소한nginx www는 301에서 https로 영구 리디렉션합니다.

<code> 
    server { 

    listen 80; 
    listen 443; 
    server_name www.example.com; 
    return 301 $scheme://example.com$request_uri; 
    } 

    server {  
    listen 80 default_server; 

    listen [::]:80 default_server ipv6only=on;  
    listen 443 ssl http2 default_server; 
    listen [::]:443 ssl http2 default_server; 

    root /var/www/html; 

    index index.php index.html index.htm; 
    server_name example.com; 

    #Password protects the test subdomain 
    ## auth_basic "Restricted Content"; 
    ## auth_basic_user_file /etc/nginx/.htpasswd; 


    # Make site accessible from https://example.com/ 

    server_name example.com; 
    include snippets/ssl-example.com.conf;   
    include snippets/ssl-params.conf;   
    location ~ /.well-known {    
    allow all; 
     }  
    location/{  
    try_files $uri $uri/ /index.php$is_args$query_string; 
    #try_files $uri $uri/ /index.php?q=$request_uri;     
    # First attempt to serve request as file, then    
    # as directory, then fall back to displaying a 404.    
    # try_files $uri $uri/ =404;    
    # Uncomment to enable naxsi on this location    
    # include /etc/nginx/naxsi.rules 
    } 
    error_page 500 502 503 504 /50x.html; 
    location = /50x.html { 

    root /usr/share/nginx/html;  
    } 
    location ~ [^/]\.php(/|$) { 
    fastcgi_split_path_info ^(.+?\.php)(/.*)$; 
    if (!-f $document_root$fastcgi_script_name) { 
    return 404; 
    } 
    # Mitigate https://httpoxy.org/ vulnerabilities 
    fastcgi_param HTTP_PROXY ""; 
    include snippets/fastcgi-php.conf; 
    fastcgi_pass unix:/run/php/php7.0-fpm.sock; 
    include fastcgi_params; 
    }  
    location ~ \.php$ { 
    #match actual filename with extension or file not found 
    #try_files $uri $uri =404; 
    include snippets/fastcgi-php.conf; 
    fastcgi_pass unix:/run/php/php7.0-fpm.sock;  
    } 

    # deny access to .htaccess files, if Apache's document root 
    # concurs with nginx's one 
    # 
    #location ~ /\.ht 
    {  
    #  deny all; 
    # 
    } 

    } 
</code> 

답변

0

:

은 여기 내 전체 Nginx에 설정 파일입니다.

server { 
    listen 80; 
    listen 443 ssl; 
    ssl_certificate  ...; 
    ssl_certificate_key ...; 

    server_name www.example.com; 
    return 301 $scheme://example.com$request_uri; 
} 

example.comwww.example.com 모두를위한 하나 개의 인증서 파일이있는 경우 ssl_xxx 지시는 모두 서버 차단에 의해 상속되는 주변 블록에 나타날 수 있습니다. 자세한 내용은 this document을 참조하십시오.

+0

하자 암호화가 나는가 또한 인증서를 추가해야 할 것 같아요, 그래서 * .example.com을 같이 와일드 카드 인증서 표시를 지원하지 않는 것을 www.example.com. 리디렉션 만하고 있습니다. – Gabriel

+0

그래서? : 서버 { 듣기 80 default_server; 듣기 [::] : 80 default_server ipv6only = 켜기; listen 443 ssl http2 default_server; 듣기 [::] : 443 ssl http2 default_server; server_name www.example.com; 에는 snippets/ssl-www.example.com.conf가 포함됩니다. 에는 snippets/ssl-params.conf가 포함됩니다. return 301 $ scheme : //example.com$request_uri; } 서버 { 수신 대기 80 default_server; 듣기 [::] : 80 default_server ipv6only = 켜기; listen 443 ssl http2 default_server; 듣기 [::] : 443 ssl http2 default_server; server_name example.com; – Gabriel

0

마지막으로 해결했습니다. Letex Encrypt 인증서에 www.example.com과 example.com을 모두 추가하여 갑자기 작동했습니다.

나는 sudo를 letsencrypt certonly -a 웹 루트 --webroot-경로는/var/www /에서 HTML -d www.example.com, example.com은

그래서의 nginx를 다시 시작한 후, www가 리디렉션 갑자기 일 = 한 ! 는 또한 하단의 리디렉션을 넣어하고 아래의 nginx 파일 변경 :

server { 
listen 443 ssl http2 default_server; 
listen [::]:443 ssl http2 default_server; 

include snippets/ssl-www.example.com.conf; 
include snippets/ssl-params.conf; 

root /var/www/html; 
index index.php index.html index.htm; 

server_name example.com; 

#Password protects the test subdomain 
## auth_basic "Restricted Content"; 
## auth_basic_user_file /etc/nginx/.htpasswd; 

location ~ /.well-known { 
allow all; 
} 
location/{ 
try_files $uri $uri/ /index.php$is_args$query_string; 
# include /etc/nginx/naxsi.rules 
} 
error_page 500 502 503 504 /50x.html; 
location = /50x.html { 
root /usr/share/nginx/html; 
} 
location ~ [^/]\.php(/|$) { 
fastcgi_split_path_info ^(.+?\.php)(/.*)$; 
if (!-f $document_root$fastcgi_script_name) { 
return 404; 
} 
# Mitigate https://httpoxy.org/ vulnerabilities 
fastcgi_param HTTP_PROXY ""; 
include snippets/fastcgi-php.conf; 
fastcgi_pass unix:/run/php/php7.0-fpm.sock; 
include fastcgi_params; 
} 
location ~ \.php$ { 
#match actual filename with extension or file not found 
#try_files $uri $uri =404; 
include snippets/fastcgi-php.conf; 
fastcgi_pass unix:/run/php/php7.0-fpm.sock; 
} 
} 
server { 
listen [::]:80 default_server ipv6only=on; 
listen 80 default_server; 
server_name www.example.com; 
return 301 https://example.com$request_uri; 
} 
관련 문제