Nginx   发布时间:2022-05-11  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了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;

大佬总结

以上是大佬教程为你收集整理的Nginx:在特定路径组件的别名内路由到index.php全部内容,希望文章能够帮你解决Nginx:在特定路径组件的别名内路由到index.php所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: