편집하다

편집하다

도메인이 있다고 가정해 보겠습니다.example.com그리고 그것은에 위치하고 있습니다www/www루트/예제. 하위 도메인도 만들었습니다api.example.comCNAME을 생성하여API가리키는example.com. 사용 목적을 위해 다음과 같은 디렉터리를 만들었습니다.API에 위치한www/www루트/API이는 확실히 내 도메인의 루트 디렉터리에 없습니다.

API 디렉토리(www/www루트/API).

다음과 같은 프로젝트가 있다고 가정합니다.데모프로젝트디렉토리(www/wwwroot/api/DemoProject) 내에 필요한 API 코드와 함께 디렉토리를 만들었습니다.

이제 내가 원하는 건 전화를 거는 것api.example.com/DemoProject디렉토리를 가리켜야 합니다 www/wwwroot/api/데모프로젝트. NGINX에서는 어떻게 할 수 있나요?

여기에는 내부의 모든 디렉토리에 액세스할 수 있도록 몇 가지 규칙이 필요합니다.www/www루트/API디렉토리 이름과 일치하는 api.example.com/DirectoryName을 호출할 때마다.

좋다:

  • api.example.com/Google다음을 가리켜야 한다www/wwwroot/api/구글
  • api.example.com/Facebook다음을 가리켜야 한다www/wwwroot/api/페이스북
  • api.example.com/Netflix다음을 가리켜야 한다www/wwwroot/api/넷플릭스

현재 example.com 도메인의 NGNIX 구성 파일은 다음과 같습니다.

server
{
    listen 80;
    listen 443 ssl http2;
    server_name example.com api.example.com;
    index index.php index.html index.htm default.php default.htm default.html;
    root /www/wwwroot/panel_ssl_site;

    #SSL-START SSL related configuration, do NOT delete or modify the next line of commented-out 404 rules
    #error_page 404/404.html;
    #HTTP_TO_HTTPS_START
    if ($server_port !~ 443){
        rewrite ^(/.*)$ https://$host$1 permanent;
    }
    #HTTP_TO_HTTPS_END
    ssl_certificate    /www/server/panel/vhost/cert/example.com/fullchain.pem;
    ssl_certificate_key    /www/server/panel/vhost/cert/example.com/privkey.pem;
    ssl_protocols TLSv1.1 TLSv1.2 TLSv1.3;
    ssl_ciphers EECDH+CHACHA20:EECDH+CHACHA20-draft:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5;
    ssl_prefer_server_ciphers on;
    ssl_session_cache shared:SSL:10m;
    ssl_session_timeout 10m;
    add_header Strict-Transport-Security "max-age=31536000";
    error_page 497  https://$host$request_uri;


    #SSL-END
    #referenced redirect rule, if commented, the configured redirect rule will be invalid
    include /www/server/panel/vhost/nginx/redirect/example.com/*.conf;

    #ERROR-PAGE-START  Error page configuration, allowed to be commented, deleted or modified
    #error_page 404 /404.html;
    #error_page 502 /502.html;
    #ERROR-PAGE-END

    #PHP-INFO-START  PHP reference configuration, allowed to be commented, deleted or modified
    #SECURITY-START Hotlink protection configuration
    location ~ .*\.(jpg|jpeg|gif|png|js|css)$
    {
        expires      30d;
        access_log off;
        valid_referers example.com;
        if ($invalid_referer){
           return 404;
        }
    }
    #SECURITY-END
    include enable-php-00.conf;
    #PHP-INFO-END

    #REWRITE-START URL rewrite rule reference, any modification will invalidate the rewrite rules set by the panel
    include /www/server/panel/vhost/rewrite/example.com.conf;
    #REWRITE-END

    # Forbidden files or directories
    location ~ ^/(\.user.ini|\.htaccess|\.git|\.svn|\.project|LICENSE|README.md)
    {
        return 404;
    }

    # Directory verification related settings for one-click application for SSL certificate
    location ~ \.well-known{
        allow all;
    }

    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
    {
        expires      30d;
        error_log /dev/null;
        access_log off;
    }

    location ~ .*\.(js|css)?$
    {
        expires      12h;
        error_log /dev/null;
        access_log off; 
    }
    access_log  /www/wwwlogs/example.com.log;
    error_log  /www/wwwlogs/example.com.error.log;
}

저는 행정업무를 처음 접했습니다. 나는 내 개인 프로젝트 중 일부를 위해 그것을하고 있습니다. 나는 이 문제를 직면하는 데 어려움을 겪고 있습니다. 단순한 문제라면 미리 사과드립니다. 또한 미리 감사드립니다.

편집하다

구성의 라인에서

server_name example.com api.example.com;

리디렉션을 활성화했기 때문에 도메인과 하위 도메인이 모두 있습니다. 누군가 api.example.com만 호출하면 example.com으로 리디렉션됩니다.

관련 정보