編輯

編輯

假設我有一個網域example.com它位於www/wwwroot/範例。我還建立了一個子網域api.example.com透過創建 CNAME應用程式介面指向example.com。出於使用目的,我創建了一個名為的目錄應用程式介面它位於www/wwwroot/api這當然不在我的網域的根目錄中。

我將在API目錄中託管不同獨立專案的API程式碼(www/wwwroot/api)。

假設我有一個名為示範項目我已經在目錄 (www/wwwroot/api/DemoProject) 中建立了一個目錄以及必要的 API 程式碼。

現在我想要的是如果我打電話api.example.com/DemoProject它應該指向目錄 www/wwwroot/api/DemoProject。我怎麼能在 NGINX 中做到這一點?

這裡我需要一些規則,以便我可以訪問裡面的任何目錄www/wwwroot/api每當我呼叫與目錄名稱相符的 api.example.com/DirectoryName 時。

喜歡:

  • api.example.com/Google應該指向www/wwwroot/api/谷歌
  • api.example.com/Facebook應該指向www/wwwroot/api/Facebook
  • api.example.com/Netflix應該指向www/wwwroot/api/Netflix

這是我目前 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。

相關內容