nginx http重定向到本地主机

时间:2019-06-19 15:51:10

标签: nginx redhat

因此,我有一个在http://localhost:3000上运行的应用程序。我想使用 nginx 将其公开给其他人。我使用以下配置将http连接重定向到https:

server {
      listen 80 default;
      server_name  www.example.com;

      return 301 https://$server_name$request_uri;
    }

问题是当我尝试访问www.example.comhttp://www.example.com时,它会将我重定向到https://localhost/而不是https://www.example.com

https://www.example.com会按预期重定向。

供您参考,www.example.com是一个内部域。

我的所有配置:

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

# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    # Disable sending the server identification
    server_tokens off;

    # Prevent displaying Botpress in an iframe (clickjacking protection)
    add_header X-Frame-Options SAMEORIGIN;

    # Prevent browsers from detecting the mimetype if not sent by the server.
    add_header X-Content-Type-Options nosniff;

    # Force enable the XSS filter for the website, in case it was disabled manually
    add_header X-XSS-Protection "1; mode=block";

    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;

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

    # Redirect unsecure requests to the HTTPS endpoint
    server {
      listen 80 default;
      server_name  www.example.com;

      return 301 https://$server_name$request_uri;
    }

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

      ssl_certificate      cert.pem;
      ssl_certificate_key  cert.key;

      # Force the use of secure protocols only
      ssl_prefer_server_ciphers on;
      ssl_protocols TLSv1 TLSv1.1 TLSv1.2;

      # Enable session cache for added performances
      ssl_session_cache shared:SSL:50m;
      ssl_session_timeout 1d;
      ssl_session_tickets off;

      # Added security with HSTS
      add_header Strict-Transport-Security "max-age=31536000; includeSubdomains; preload";

      # We need to add specific headers so the websockets can be set up through the reverse proxy
      location /socket.io/ {
        proxy_pass http://localhost:3000/socket.io/;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "Upgrade";
      }

      # All other requests should be directed to the server
      location / {
        proxy_pass http://localhost:3000;
      }
    }
}

在此先感谢您的帮助,

1 个答案:

答案 0 :(得分:0)

正如@johnsing在评论部分中所述,删除default并清除缓存即可完成这项工作。