首页 > 解决方案 > Nginx 子域重定向不起作用,所有请求都转到同一个端口

问题描述

Nginx 将 http 和 https 请求重定向到端口 3000。但我试图将子域重定向到端口 3001 没有成功,它被重定向到端口 3000。没有登录错误。即使我在端口 80 上尝试 http 请求,我也无法重定向端口 3001,所有请求都会转到 3000。

我用子域配置了 DNS。

我的 nginx.conf 文件是:

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;

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;

  keepalive_timeout  65;

  #gzip  on;

  server {
      listen       443 ssl http2 default_server;
      listen       [::]:443 ssl http2 default_server;
      server_name  example.com www.example.com;
      root         /usr/share/nginx/html;

      ssl_certificate "/etc/letsencrypt/live/example.com/fullchain.pem";
      ssl_certificate_key "/etc/letsencrypt/live/example.com/privkey.pem";
      ssl_session_cache shared:SSL:1m;
      ssl_session_timeout  10m;
      ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
      ssl_ciphers HIGH:SEED:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!PSK:!RSAPSK:!aDH:!aECDH:!EDH-DSS-DES-CBC3-SHA:!KRB5-DES-CBC3-SHA:!SRP;
      ssl_prefer_server_ciphers on;

      location / {
          proxy_pass http://127.0.0.1:3000;
      }

      error_page 404 /404.html;
          location = /40x.html {
      }

      error_page 500 502 503 504 /50x.html;
          location = /50x.html {
      }
  }

  server {
      listen 80 default_server;
      listen [::]:80 default_server;
      server_name example.com.br www.example.com;
      return 301 https://example.com;
  }


  ############## THIS PART OF THE CODE IS IGNORED ###################
  server {
      listen 80;
      listen [::]:80; 
      server_name subdomain.example.com;

      location / {
          proxy_pass http://127.0.0.1:3001;
      }
  }
  ###################################################################
}

原来这不是 nginx 的问题,我只需要删除浏览器缓存。

标签: nginxserverproxysubdomainreverse-proxy

解决方案


一切都是正确的。我只需要删除浏览器缓存。


推荐阅读