Nginx:在特定路径组件的别名内路由到index.php

前端之家收集整理的这篇文章主要介绍了Nginx:在特定路径组件的别名内路由到index.php前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我正在尝试将Nginx路由到特定路径的别名中的index.PHP.使用子域(并添加具有不同名称的服务器块)会更容易,但我的项目需要这种结构有几个非主题原因.简单地说,我想要以下内容

1. www.example.com         --> /home/user/www/public/index.PHP
2. www.example.com/a/b     --> /home/user/www/public/index.PHP
3. www.example.com/api     --> /home/user/api/public/index.PHP
4. www.example.com/api/a/b --> /home/user/api/public/index.PHP

数字1和2非常标准:始终提供index.PHP文件(在某个根目录中)并让PHP处理路由. 3号模拟子域构造(并应指向不同目录中的index.PHP).

我已经设法让数字1-3工作,但不是数字4.每当我去www.example.com/api/a/b它给出404.错误日志告诉我Nginx试图为我服务/家庭/用户/ API /公共// A / b /.我觉得我非常接近解决方案,但我似乎没有做对.我也可以以相同的方式提供静态文件.

以下配置是相关的:

server {
    listen 80;
    server_name example.com www.example.com;
    root /home/user/www/public;

    index index.PHP index.html;

    location / {
        try_files $uri $uri/ /index.PHP;
    }
    location ~ \.PHP${
            try_files $uri /index.PHP =404;
            fastcgi_split_path_info ^(.+\.PHP)(/.+)$;
            fastcgi_index   index.PHP;
            fastcgi_pass    unix:/var/run/PHP5-fpm.sock;
            include         fastcgi_params;
            fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;
    } 
    location /api {
        alias /home/user/api/public/;

        location ~ \.PHP${
           try_files $uri /index.PHP =404;
           fastcgi_split_path_info ^(.+\.PHP)(/.+)$;
           fastcgi_index   index.PHP;
           fastcgi_pass    unix:/var/run/PHP5-fpm.sock;
           include         fastcgi_params;
           fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;
         }
    }      
}

提前致谢.

server {
    listen 80;
    server_name example.com www.example.com;
    root /home/user/www/public;

    index index.PHP index.html;

    location / {
        try_files $uri $uri/ /index.PHP;
    }
    location ~ \.PHP${
            try_files $uri /index.PHP =404;
            fastcgi_split_path_info ^(.+\.PHP)(/.+)$;
            fastcgi_index   index.PHP;
            fastcgi_pass    unix:/var/run/PHP5-fpm.sock;
            include         fastcgi_params;
            fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;
    } 
    location /api/ {
        alias /home/user/api/public/;
        try_files $uri /api/index.PHP =404;

        location ~ \.PHP${
           try_files $uri /index.PHP =404;
           fastcgi_split_path_info ^(.+\.PHP)(/.+)$;
           fastcgi_index   index.PHP;
           fastcgi_pass    unix:/var/run/PHP5-fpm.sock;
           include         fastcgi_params;
           fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;
         }
    }      
}
你很亲密.但是您需要匹配位置及其相应别名的尾随/字符.

您还需要在该块中添加try_files以引用正确的index.PHP,这可能是/api/index.PHP.

    location /api/ {
        alias /home/user/api/public/;
        try_files $uri /api/index.PHP;
原文链接:https://www.f2er.com/nginx/435578.html

猜你在找的Nginx相关文章