簡體   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