我的网站无需 SSL 即可通过 http 访问,但无法通过 https 访问。https://www.example.com发生网络超时。此外,nginx error.log 未显示任何错误。为什么我无法通过 https 重定向并访问网站?
这是我的 nginx 服务器块文件:
server {
listen 80;
listen [::]:80;
server_name www.example.com example.com example.org www.example.org
return 301 https://$server_name$request_uri;
}
server {
# SSL configuration
listen 443 ssl;
listen [::]:443 ssl;
ssl on;
ssl_certificate /etc/letsencrypt/live/www.example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/www.example.com/privkey.pem;
root /var/www/example;
# Add index.php to the list if you are using PHP
index index.php index.html index.htm index.nginx-debian.html;
server_name example.com www.example.com;
# pass the PHP scripts to FastCGI server
#
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
# With php5-fpm:
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /\.ht {
deny all;
}
}
netstat -tulpen|grep 443 的输出
tcp 0 0 0.0.0.0:443 0.0.0.0:* LISTEN 0 417562386 339/nginx: worker p
tcp6 0 0 :::443 :::* LISTEN 0 417562387 339/nginx: worker p
编辑:这是我的 nginx.conf
user xy;
worker_processes 5;
pid /run/nginx.pid;
events {
worker_connections 4096;
# multi_accept on;
}
http {
##
# Basic Settings
##
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
# server_tokens off;
# server_names_hash_bucket_size 64;
# server_name_in_redirect off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
##
# SSL Settings
##
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;
##
# Logging Settings
##
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
##
# Gzip Settings
##
gzip on;
gzip_disable "msie6";
# gzip_vary on;
# gzip_proxied any;
# gzip_comp_level 6;
# gzip_buffers 16 8k;
# gzip_http_version 1.1;
# gzip_types text/plain text/css application/json application/javascript text/xml application/$
##
# Virtual Host Configs
##
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
答案1
使用该配置,您的网站将无法通过 HTTP 访问。如果仍然可以访问,则不会使用该配置。
首先,尝试停止 nginx,然后重新启动。有时它会挂起并且无法正确重新启动。
然后检查一下,你的网站是否真的能从配置的这一部分正常工作。添加 access_log 指令,将请求记录到一些不常用的(对于你的配置而言)文件中。比如 /tmp/nginx.test.log(只是为了避免写权限处理)。
http://nginx.org/en/docs/http/ngx_http_log_module.html#access_log
如果您正在访问您的网站并且没有任何行添加到日志中,那么它只是由配置的其他部分提供服务。 检查后,不要忘记删除该日志并自行记录!
答案2
好吧,我终于找到了。这只是防火墙的一个故障。端口 443 不被允许。但它仍然没有从 http:// 重定向到 https://