【问题标题】:400 Bad Request on nginx 1.14.0 Ubuntunginx 1.14.0 Ubuntu 上的 400 错误请求
【发布时间】:2020-06-06 15:56:15
【问题描述】:

场景:

我有两个网站admin.example.comwww.example.com。 admin 网站使用 Django 2.2 开发,前端网站使用 Angular 8 开发。两个网站都在 Ubuntu 18 上配置了 Nginx 1.14.0。

问题:

我最近在这些网站上添加了 SSL。虽然管理站点运行良好,但前端显示 400 bad request 错误并带有文本“普通 HTTP 请求已发送到 HTTPS 端口”。

我发现这个Dealing with nginx 400 "The plain HTTP request was sent to HTTPS port" error 解决了同样的问题,但它不适用于我的情况。

我对 admin.example.com (Django) 的配置:

server {
    listen 80;
    server_name admin.example.com;
    return 301 https://admin.example.com$request_uri;
}
server {
    listen 443 ssl;
    server_name admin.example.com;
    ssl off;
    ssl_certificate /root/ssl-bundle.crt;
    ssl_certificate_key /root/example.com.key;

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /home/raakesh/backend;
    }

    location / {
        include proxy_params;
        proxy_pass http://unix:/run/gunicorn.sock;
    }
}

example.com 的配置(Angular):

server {
    listen 80 default_server;

    server_name www.example.com;
    return 301 https://$server_name$request_uri;
}

server {
    listen 443 default_server ssl;
    server_name www.example.com;
    root /home/rohangular/websiteangular/dist/angular-website;
    index index.html index.htm;

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

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

    location = /favicon.ico { access_log off; log_not_found off; }
    ssl off;
    ssl_certificate /root/ssl-bundle.crt;
    ssl_certificate_key /root/example.com.key;
}

我还尝试使用以下方法记录此信息:

error_log /var/log/nginx/example.com/error.log 信息;

并在日志文件中找到:

[info] 1533#1533: *247 客户端在读取客户端请求标头时向 HTTPS 端口发送纯 HTTP 请求,客户端:188.166.28.57,服务器:www.example.com,请求:“GET /main-es5.54e5eba613f299b8fae5 .js HTTP/1.1”,主机:“www.example.com”

我面临的另一个有线问题是,这个 example.com 网站有时会自动开始工作,有时会显示 400 错误,如上所述。

当第一个工作时,我无法理解,而第二个工作则不然。如果我做错了什么或遗漏了什么,请帮助我。

【问题讨论】:

  • 除此之外:你应该更新你的 nginx

标签: javascript angular nginx ubuntu-18.04


【解决方案1】:

该错误表示您正在尝试访问http://example.com:443,这意味着您发送的是 HTTP 请求而不是 https 请求,可能是错字还是浏览器问题?
另外,根据 nginx 文档 ssl off;nginx 1.15 之后已经过时,所以我会摆脱它.

添加以下内容很可能会解决您的问题:

if ($scheme = http) {
           return 301 https://$server_name$request_uri;
    }

最终配置应如下所示:

server {
    listen 443 default_server ssl;
    server_name www.example.com;

    if ($scheme = http) {
        return 301 https://$server_name$request_uri;
    }

    root /home/rohangular/websiteangular/dist/angular-website;
    index index.html index.htm;

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

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

    location = /favicon.ico { access_log off; log_not_found off; }
    ssl_certificate /root/ssl-bundle.crt;
    ssl_certificate_key /root/example.com.key;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-05
    • 2014-02-19
    • 1970-01-01
    • 2014-08-03
    • 2016-04-07
    • 1970-01-01
    • 1970-01-01
    • 2019-12-13
    相关资源
    最近更新 更多