Nginx中的“纯HTTP请求发送到HTTPS端口”错误

2019-03-22 09:11:50 7370

Nginx的示例配置

server{
listen 80;
server_name example.com www.landui.com;
return 301 https://www.landui.com$request_uri;
}
server {
listen 443 ssl http2;
server_name example.com www.landui.com;
root   /var/www/html/example.com/;
index index.php index.html index.htm;
#charset koi8-r;
access_log /var/log/nginx/example.com/example.com_access_log;
error_log   /var/log/nginx/example.com/example.com_error_log   error;
# SSL/TLS configs
ssl on;
ssl_certificate /etc/ssl/certs/example_com_cert_chain.crt;
ssl_certificate_key /etc/ssl/private/example_com.key;
include /etc/nginx/ssl.d/ssl.conf;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
error_page   500 502 503 504  /50x.html;
location = /50x.html {
root   /var/www/html/example.com/;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
#    proxy_pass   http://www.landui.com;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
root   /var/www/html/example.com/;
fastcgi_pass   127.0.0.1:9001;
#fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index  index.php;
fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
include         fastcgi_params;
include /etc/nginx/fastcgi_params;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
#    deny  all;
#}
}

使用上述配置,一旦客户端尝试通过端口80(即http://www.landui.com访问您的站点,相关错误将显示在下面的屏幕截图中。

image.png

Nginx的404错误请求错误


您遇到此错误,因为每当客户端尝试通过HTTP访问您的站点时,请求都被重定向到HTTPS。 这是因为nginx希望在事务中使用SSL,但原始请求t(通过端口80接收)是普通的HTTP,它抱怨错误。


另一方面,如果客户端使用https://www.landui.com ,他们将不会遇到上述错误。 另外,如果你有其他网站配置为不使用SSL,nginx会默认使用HTTPS,导致上述错误。


要解决这个错误,请在您的配置中将下面的行注释掉,或者将其设置为off。


#ssl on 

OR

ssl off

保存并关闭文件。 然后重新启动nginx服务。


# systemctl restart nginx

OR

$ sudo systemctl restart nginx


提交成功!非常感谢您的反馈,我们会继续努力做到更好!

这条文档是否有帮助解决问题?

非常抱歉未能帮助到您。为了给您提供更好的服务,我们很需要您进一步的反馈信息:

在文档使用中是否遇到以下问题: