繁体   English   中英

nginx http重定向到本地主机

[英]nginx http redirect to localhost

所以我有一个运行在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;
      }
    }
}

预先感谢您的帮助,

正如@johnsing在注释部分中所述,删除default并清除缓存可以完成此工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM