2017-01-18 8 views
0

dauerhaft umleiten Ich versuche, www.example.com um https://example.com umleiten, indem Sie den ersten Serverblock hinzufügen. Aber es wird nicht umgeleitet. Und es muss ständig umgeleitet werden, um SEO-Probleme und Sicherheitswarnungen für den Benutzer zu vermeiden.Nginx Ww soll mit 301 unter https

Hier ist meine komplette NGINX Konfigurationsdatei:

<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> 

Antwort

0

Als Minimum Serverblock den ssl Schlüsselwort fehlt https auf dem 443-Port und die Zertifikatsdefinitionen zu ermöglichen.

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

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

Wenn Sie nur eine Zertifikatsdatei sowohl für haben example.com und www.example.com, die ssl_xxx Richtlinien im umgebenden Block erscheinen kann von beiden Server Blöcke vererbt werden. Siehe this document für mehr.

+0

Lassen Sie uns encrypt scheint nicht Wildcard certs wie * .example.com zu unterstützen, damit ich denke, ich habe auch ein Zertifikat hinzufügen für www.example.com obwohl ich es gerade umadressiere. – Gabriel

+0

So ?: Server { hören 80 default_server; listen [::]: 80 default_server ipv6only = on; listen 443 ssl http2 default_server; listen [::]: 443 ssl http2 default_server; Servername www.beispiel.com; enthalten Snippets/ssl-www.example.com.conf; enthalten Snippets/ssl-params.conf; Rückgabe 301 $ Schema: //beispiel.com$request_uri; } Server { hören 80 default_server; listen [::]: 80 default_server ipv6only = on; listen 443 ssl http2 default_server; listen [::]: 443 ssl http2 default_server; Servername Beispiel.com; – Gabriel

0

Ich habe es endlich gelöst. Durch Hinzufügen von www.example.com und example.com zum Let's Encrypt-Zertifikat funktionierte es plötzlich.

habe ich sudo letsencrypt certonly -a Webroot --webroot-path =/var/www/html -d www.example.com, example.com

So nach nginx Neustart der www Redirect plötzlich gearbeitet ! Ich habe auch die Umleitung auf der Unterseite und verändert, wie unten die nginx-Datei:

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; 
} 
Verwandte Themen