【问题标题】:Nginx not redirecting traffic from http to httpsNginx 不会将流量从 http 重定向到 https
【发布时间】:2019-08-26 15:45:29
【问题描述】:

我在 Amazon Linux 2 上的 EC2 服务器上安装了 nginx,并通过 HTTP 和 HTTPS 为我的网站提供服务。但是,我无法让位置块在 HTTP 上工作。如果我需要提供文件或重定向到 proxy_path,它们可以在 HTTPS 上完美运行,但不适用于 HTTP。

我想将所有流量从 HTTP 重定向到 HTTPS。

此外,即使我删除了第一个服务器块,网站仍然通过端口 80 提供服务。

server {
        listen 80;
        server_name *.example.com;
        return 301 https://$host$request_uri;
    }

这是我的完整 nginx 配置文件,位于 /etc/nginx/nginx.conf

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    #include /etc/nginx/conf.d/*.conf;

    server {
        listen 80;
        server_name *.example.com;
        return 301 https://$host$request_uri;
    }

    server {
        listen 443 ssl;

        server_name *.example.com;

        ssl_certificate     /etc/letsencrypt/live/example.com/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;

        location /.well-known/acme-challenge/test {
                return 200 'test.test';
        }
        location / {
            proxy_pass http://localhost:5000;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection 'upgrade';
            proxy_set_header Host $host;
            proxy_cache_bypass $http_upgrade;
        }
    }
}

是否有 nginx 正在使用的默认配置文件?

【问题讨论】:

    标签: amazon-web-services nginx amazon-ec2 amazon-linux


    【解决方案1】:

    我解决了这个问题。主要问题是另一个进程缓存请求并在主机上运行并侦听端口 80 和 443。杀死该进程,一切正常。

    【讨论】:

      【解决方案2】:

      您的 http 块应如下所示:

      server {
          listen 80;
          server_name *.example.com;
      
          location / {
              return 301 https://$host$request_uri;
          }
      }
      

      【讨论】:

      • 我已经修改了 nginx.conf 文件,我仍然被发送到 HTTP。我没有被重定向到 HTTPS。看起来是否有一个默认的 HTTP 配置文件被 NGINX 用作默认配置。
      猜你喜欢
      • 1970-01-01
      • 2016-07-27
      • 2017-10-16
      • 1970-01-01
      • 2011-03-29
      • 2018-01-05
      • 1970-01-01
      • 2018-09-30
      • 2016-03-18
      相关资源
      最近更新 更多