假设我有一个域名示例.com它位于www/wwwroot/示例。我还创建了一个子域名api.example.com通过创建 CNAMEapi指向示例.com。为了方便使用,我创建了一个名为api位于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/Google
- 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。