尝试使用https配置设置nginx返回无效的主机头

时间:2018-12-11 14:40:37

标签: nginx nginx-reverse-proxy nginx-config

我正在尝试使用https配置nginx,以便我可以浏览到它(www.oidctest.com-(在hosts文件中配置为localhost),它将重定向到我的应用程序,位于:www.oidctest.com: 3000

我的nginx配置是:

    server {
        listen 443 ssl http2;
        listen [::]:443 ssl http2;
        server_name     www.oidctest.com;
        return 301 http://$server_name:3000$request_uri;
        # certs sent to the client in SERVER HELLO are concatenated in
        ssl_certificate
        ssl_certificate /nginx-1.14.1/conf/server.crt;
        ssl_certificate_key /nginx-1.14.1/conf/server.key;
        ssl_session_timeout 1d;
        ssl_session_cache shared:SSL:50m;
        ssl_session_tickets off;

        # Diffie-Hellman parameter for DHE ciphersuites, recommended 2048 bits
        #ssl_dhparam /path/to/dhparam.pem;

        # intermediate configuration. tweak to your needs.
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_ciphers 'ECDHE....HA:!DSS';
        ssl_prefer_server_ciphers on;

        # HSTS (ngx_http_headers_module is required) (15768000 seconds = 6 months)
        #add_header Strict-Transport-Security max-age=15768000;

        # OCSP Stapling ---
        # fetch OCSP records from URL in ssl_certificate and cache them
        ssl_stapling on;
        ssl_stapling_verify on;

        ## verify chain of trust of OCSP response using Root CA and Intermediate certs
        #ssl_trusted_certificate /path/to/root_CA_cert_plus_intermediates;

        resolver 8.8.8.8;

         location / {
            proxy_pass http://localhost:3000;
            proxy_redirect off;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
            proxy_set_header Host $host;
        }
    }
}

但是当我浏览到“ www.oidctest.com”时,我得到:“无效的主机标头”

您可以建议解决方案吗?

1 个答案:

答案 0 :(得分:0)

在nginx.conf中,我将替换

proxy_pass http://localhost:3000;

使用

proxy_pass http://127.0.0.1:3000;

但更重要的是,该错误与您的ReactJS应用有关(我想这是其中之一吗?)。您可以在webpack.config.js中的devServer下添加allowedHosts:

devServer: {
  compress: true,
  inline: true,
  port: '8080',
  allowedHosts: [
      '.amazonaws.com'
  ]
},