Файлы обслуживаются как HTTP вместо HTTPS
Я только что настроил сервер nginx и letsencrypt, и я думаю, что некоторые изменения certbot
сделал мой конфиг испортил вещи.
Я хочу, чтобы мой веб-сайт по умолчанию использовал HTTPS и обслуживал все файлы с HTTPS, чтобы избежать ошибок смешанного содержимого, таких как:
Mixed Content: The page at 'https://example.org/login' was loaded over HTTPS, but requested an insecure stylesheet 'http://example.org/css/auth.css?v=1'. This request has been blocked; the content must be served over HTTPS.
Вот мой конфиг Nginx - я обеспокоен уровнем дублирования, который я вижу с момента добавления сертификата SSL, но недостаточно опытным, чтобы понять, не вызывает ли это каких-либо проблем. Я заменил свой домен на "example.org".
server {
listen 80;
listen 443 default_server ssl;
listen [::]:443 ssl ipv6only=on;
ssl_certificate /etc/letsencrypt/live/example.org/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/example.org/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
server_name example.org;
charset utf-8;
root /home/www/example.org/public;
index index.php index.html index.htm;
gzip on;
gzip_vary on;
gzip_disable "msie6";
gzip_comp_level 6;
gzip_min_length 1100;
gzip_buffers 16 8k;
gzip_proxied any;
gzip_types
text/plain
text/css
text/js
text/xml
text/javascript
application/javascript
application/x-javascript
application/json
application/xml
application/xml+rss;
location / {
try_files $uri $uri/ /index.php?$query_string;
autoindex on;
}
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/run/php/php7.2-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~* \.(?:jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm|htc|svg|woff|woff2|ttf) {
expires 1M;
access_log off;
add_header Cache-Control "public";
}
location ~* \.(?:css|js) {
expires 7d;
access_log off;
add_header Cache-Control "public";
}
location ~ /\.ht {
deny all;
}
}
server {
root /home/www/example.org/public;
index index.php index.html index.htm;
server_name example.org www.example.org; # managed by Certbot
charset utf-8;
gzip on;
gzip_vary on;
gzip_disable "msie6";
gzip_comp_level 6;
gzip_min_length 1100;
gzip_buffers 16 8k;
gzip_proxied any;
gzip_types
text/plain
text/css
text/js
text/xml
text/javascript
application/javascript
application/x-javascript
application/json
application/xml
application/xml+rss;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/run/php/php7.2-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~* \.(?:jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm|htc|svg|woff|woff2|ttf) {
expires 1M;
access_log off;
add_header Cache-Control "public";
}
location ~* \.(?:css|js) {
expires 7d;
access_log off;
add_header Cache-Control "public";
}
location ~ /\.ht {
deny all;
}
}
server {
if ($host = www.example.org) {
return 301 https://$host$request_uri;
} # managed by Certbot
if ($host = example.org) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80 ;
listen [::]:80 ;
server_name example.org www.example.org;
return 404; # managed by Certbot
}
2 ответа
Не могу найти элегантное решение самостоятельно. "Грязное решение" состояло в том, чтобы включить cloudflare на домене, а затем установить Always Use HTTPS: ON
а также Automatic HTTPS Rewrites: ON
Почему бы тебе не попробовать изменить это
if ($host = example.org) {
return 301 https://$host$request_uri;
к этому
if ($scheme = http) {
return 301 https://example.org$request_uri;
изменить другое перенаправление также