簡體   English   中英

將NGINX http重定向到https

[英]Redirect NGINX http to https

我在我的域memorisemedicine.com的Nginx服務器上使用LetsEncrypt。

我在/ etc / nginx / sites-available中的“ memorise-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;
 }

在您的nginx配置中,我看不到偵聽ssl的端口443的服務器部分。 我將添加我的配置,以便您可以根據自己的需要對其進行編輯:

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; 包括從certbotfullchain私人

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM