nginx location代码块下放了个子目录,里面的index.php报404

背景描述

我想在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是可以访问的。

求大神解救~

阅读 3.2k
1 个回答
新手上路,请多包涵
location ^~ /qr {
        alias /Users/whj/web/qr/public;
        try_files $uri $uri/ @qr;
        index index.php index.html index.htm;

        location ~ \.php$ {
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass unix:/tmp/php-cgi.sock;
            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME /Users/whj/web/qr/public/index.php;
        }
    }

    location @qr {
        rewrite /qr/(.*)$ /qr/index.php?/$1 last;
    }

这是我localhost下子目录qr的nginx配置,你可以参考下

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题