2017-07-30 223 views
0

我在我的域名memorisemedicine.com上使用了Nginx服務器上的LetsEncrypt。將NGINX http重定向到https

我在位於/ etc/nginx/sites-available的'memorize-frontend.conf'文件末尾添加了一個服務器塊,該文件應該將非SSL流量重定向到SSL(https)。

現在,當我不使用https://訪問網站時,我注意到有時我現在正確地重定向了。但特別是在Firefox中,這對我不起作用&我仍然可以訪問此域上的http://頁面,這是我永遠不可能實現的。有人知道我的.conf文件有什麼問題嗎?我也嘗試編輯nginx.conf文件,但它似乎並沒有很好地使用服務器模塊。

server { 
charset utf-8; 
client_max_body_size 128M; 

listen 80; ## listen for ipv4 
#listen [::]:80 default_server ipv6only=on; ## listen for ipv6 



server_name memorisemedicine.com www.memorisemedicine.com; 


root  /srv/memorise/frontend/web; 
index  index.php; 



# access_log /path/to/basic/log/access.log; 
# error_log /path/to/basic/log/error.log; 

location/{ 
    # Redirect everything that isn't a real file to index.php 
    try_files $uri $uri/ /index.php$is_args$args; 
} 

# uncomment to avoid processing of calls to non-existing static files by Yii 
#location ~ \.(js|css|png|jpg|gif|swf|ico|pdf|mov|fla|zip|rar)$ { 
# try_files $uri =404; 
#} 
#error_page 404 /404.html; 

# deny accessing php files for the /assets directory 
location ~ ^/assets/.*\.php$ { 
    deny all; 
} 

location ~ \.php$ { 
    include fastcgi_params; 
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 
    #fastcgi_pass unix:/var/run/php5-fpm.sock; 
    fastcgi_pass unix:/run/php/php7.0-fpm.sock; 
    try_files $uri =404; 
} 

location ~* /\. { 
    deny all; 
} 

listen 443 ssl; # managed by Certbot 
ssl_certificate /etc/letsencrypt/live/memorisemedicine.com/fullchain.pem; # man$ 
ssl_certificate_key /etc/letsencrypt/live/memorisemedicine.com/privkey.pem; # m$ 
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot 

} 

server { 
return 301 https://memorisemedicine.com$request_uri; 
} 
+0

您的主服務器模塊正在監聽端口80和443 - 您應該將「listen 80」移動到其他服務器模塊。 –

+0

嘿理查德,感謝您的幫助。你是對的,我發現了一些與443一起使用的「listen」實例,並且我刪除了那些服務器塊,因爲它們不是必需的。似乎我仍然沒有被重定向到網站的SSL版本.. –

+0

回來我想我知道你的意思 - 我添加了'聽80;'使整行'服務器{805;}}聽聽80; return 301 https://memorisemedicine.com$request_uri;並且它仍然不會重定向。我的普通http流量應該全部通過80 –

回答

0

在你的nginx的配置我不能看到它會監聽端口443的SSL服務器的一部分。 我將添加我的配置,以便可以編輯它自己的需要:

server { 
    listen 80; 
    listen [::]:80; 

    return 301 https://$server_name$request_uri; 

    access_log /var/log/nginx/api.example.com-access.log timed; 
    error_log /var/log/nginx/api.example.com-error.log; 

    root /var/www/example/html/public; 

    server_name api.example.com; 

    location/{ 
     index index.html index.php; 
     try_files $uri $uri/ @php; 
    } 

    location ~ \.php$ { 
     fastcgi_pass unix:/var/run/php/php7.0-fpm.sock; 
     fastcgi_param SCRIPT_FILENAME /var/www/example/html/public/index.php; 
     include fastcgi_params; 
    } 

    location ~ /.well-known { 
       allow all; 
     } 

} 

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

    access_log /var/log/nginx/api.example.com-access.log timed; 
    error_log /var/log/nginx/api.example.com-error.log; 

    root /var/www/example/html/public; 

    server_name api.example.com; 

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

    location/{ 
     index index.html index.php; 
     try_files $uri $uri/ /index.php?q=$uri&$args; 
    } 

    location ~ \.php$ { 
     fastcgi_pass unix:/var/run/php/php7.0-fpm.sock; 
     fastcgi_param SCRIPT_FILENAME /var/www/example/html/public/index.php; 
     include fastcgi_params; 
    } 

    location ~ /.well-known { 
       allow all; 
     } 

} 

我有段/ SSL-api.example.com.conf;包括完整鏈私有certbot