背景描述
我想在api.xxx.com域名下放个项目接口,以子目录的形式。比如api.xxx.com/friends,这个子目录下都是friends这个项目的接口。
相关代码
我的对应的nginx配置如下:
/etc/nginx/conf.d/api.conf
server {
server_name api.worldispoweredbymywife.top;
root /var/www/api;
location /friends/ {
alias /var/www/api/friends/public/;
index index.php;
try_files $uri =404;
}
location ~ /friends/.+\.php$ {
try_files $uri =404;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME /var/www/api/friends/public$fastcgi_script_name;
}
error_page 404 /404.html;
location /404.html {
root /usr/local/nginx/html/;
}
error_page 500 502 503 504 /50x.html;
location =/50x.html {
root /usr/local/nginx/html/;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
# managed by Certbot
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/api.worldispoweredbymywife.top/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/api.worldispoweredbymywife.top/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
server {
if ($host = api.worldispoweredbymywife.top) {
return 301 https://$host$request_uri;
} # managed by Certbot
server_name api.worldispoweredbymywife.top;
listen 80;
return 404; # managed by Certbot
}
当前问题
访问https://api.worldispoweredbymywife.top/friends会显示nginx自带的404页面。
ps:
1.我在/var/www/api/friends/public/下放了个index.html然后通过https://api.worldispoweredbymywife.top/index.html是可以访问的
2.我把friends的两个location注释掉,直接通过https://api.worldispoweredbymywife.top/friends/public/index.php是可以访问的。
求大神解救~