CentOS에서 Nginx의 루트 도메인으로 하위 도메인을 어떻게 리디렉션합니까?

CentOS에서 Nginx의 루트 도메인으로 하위 도메인을 어떻게 리디렉션합니까?

저는 Nginx 및 Puma와 함께 Centos를 사용하고 있습니다. 모든 하위 도메인을 기본 루트 도메인으로 리디렉션하고 싶기 때문에 여기 지침을 따랐습니다.https://stackoverflow.com/questions/26801479/nginx-redirect-all-subdomains-to-main-domain. 그러나 나는 그것을 작동시킬 수 없습니다. 아래는 내 구성입니다

upstream projecta {
  server unix:///home/rails/projecta_production/shared/sockets/puma.sock;
}

server {
  listen 80;
  server_name mydomein.com;
  return 301 http://mydomein.com$request_uri;
  root /home/rails/projecta_production/public; # I assume your app is located at this location

  location / {
    proxy_pass http://projecta; # match the name of upstream directive which is defined above
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  }

  location ~* ^/assets/ {
    # Per RFC2616 - 1 year maximum expiry
    expires 1y;
    add_header Cache-Control public;

    # Some browsers still send conditional-GET requests if there's a
    # Last-Modified header or an ETag header even if they haven't
    # reached the expiry date sent in the Expires header.
    add_header Last-Modified "";
    add_header ETag "";
    break;
  }
}

"return 301"을 제외하면http://mydomein.com$request_uri;" 줄을 입력하면 내 사이트는 루트 도메인에서는 작동하지만 하위 도메인에서는 작동하지 않습니다(예: 하위 도메인을 보면 기본 Nginx 인덱스 페이지가 표시됩니다). 모든 하위 도메인을 내 기본 도메인으로 리디렉션하는 방법 Rails/Puma 구성을 보존하시겠습니까?

답변1

현재 리디렉션을 위해 apex 도메인 가상 호스트에서 수신 대기 중입니다. 당신이 해야 할 일은 정점으로 리디렉션되는 별도의 가상 호스트 수신기를 갖는 것입니다. 다음은 apex 도메인 정의로 리디렉션되는 와일드카드 리스너의 예입니다.

upstream projecta {
  server unix:///home/rails/projecta_production/shared/sockets/puma.sock;
}

# Listener for all subdomains
server {
  listen 80;
  server_name *.mydomein.com;
  # If you want to redirect all requests, not just subdomains, use below config instead.
  # server_name _;
  return 301 http://mydomein.com$request_uri;
}

# Listener for Apex Domain
server {
  listen 80;
  server_name mydomein.com;
  root /home/rails/projecta_production/public; # I assume your app is located at this location

  location / {
    proxy_pass http://projecta; # match the name of upstream directive which is defined above
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  }

  location ~* ^/assets/ {
    # Per RFC2616 - 1 year maximum expiry
    expires 1y;
    add_header Cache-Control public;

    # Some browsers still send conditional-GET requests if there's a
    # Last-Modified header or an ETag header even if they haven't
    # reached the expiry date sent in the Expires header.
    add_header Last-Modified "";
    add_header ETag "";
    break;
  }
}

관련 정보