dongling2038 2018-08-25 15:19
浏览 99
已采纳

Nginx配置 - 在非常自定义的设置上将路由传递给index.php

Something tells me that, the way I went about this from the start is wrong, which is making it difficult for me to solve this new problem... But here we go!

The Setup

    location ~ ^/dev/([^\/]+?)\/(.*) {
            alias /opt/dev/$1/www/$2;
            autoindex on;

            # PHP location check for personal dev environments
            location ~ ^/dev/([^\/]+?)\/([^\/].+\/|)(.+\.php)$ {
                    include snippets/fastcgi-php.conf;
                    fastcgi_param SCRIPT_FILENAME $document_root$3;
                    fastcgi_param PHP_VALUE error_log=/opt/dev/$1/logs/php_errors.log;
                    fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
            }
    }

This setup above allows my devs to each have their own personal development environment. Going to: http://site_url/dev/[user]/[project]/ works just fine, if there are php files in that project directory, which locally would be in /opt/dev/[user]/www/[project]/ directory. This alone took me quite a bit of time to figure out, but now we have a new dillema that has been kicking my ass for 8 straight hours now.

Note: Regex is my weakness in programming, so the fact that nginx configuration for such things are regex based is killing me sofly

The Problem

I need to be able to detect when a path beyond the [project] level is unreachable, and then redirect that request to index.php on the project level, WITH a route param, so that we can handle it on the php level.

For example:

http://site_url/dev/[user]/[project]/something/else/

If the directory /opt/dev/[user]/www/[project]/something/else does not exist (or there's no php files in it), I need the request to go to /opt/dev/[user]/www/[project]/index.php, but also I want to call index.php with a "route" param that gives the rest of the url.

(i.e: index.php?route=something/else )

I hope this makes sense and i reeeeeeally hope someone can help me here. I acknowledge that this might mean changing my config entirely, but I don't care as long as it works.

Full Source

This is my whole entire config file from top to bottom:

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    root /var/www;

    index index.php index.html index.htm index.nginx-debian.html;

    server_name 192.168.20.200;

    location / {
            try_files $uri $uri/ =404;
    }

    location ~ ^/dev/([^\/]+?)\/(.*) {
            alias /opt/dev/$1/www/$2;
            autoindex on;

            # PHP location check for personal dev environments
            location ~ ^/dev/([^\/]+?)\/([^\/].+\/|)(.+\.php)$ {                
                    include snippets/fastcgi-php.conf;
                    fastcgi_param SCRIPT_FILENAME $document_root$3;
                    fastcgi_param PHP_VALUE error_log=/opt/dev/$1/logs/php_errors.log;
                    fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
            }
    }

    location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    location ~ /\.ht {
            deny all;
    }
}
  • 写回答

1条回答 默认 最新

  • douai2499 2018-08-26 17:16
    关注

    Sounds like what you are trying to do is try_files Serve the file if it exists, try the next if it doesn't. Something like this should work, I'm using a third regex block to capture your route variable, which I'm passing in the headers.

    location ~ ^/dev/(\w+)\/((?1))(.*) {
        root /opt;
        autoindex on;
        add_header x-route $3;
        try_files  /dev/$1/www/$2$3/index.php /dev/$1/www/$2$3.php /dev/$1/www/$2$3/ /dev/$1/www/$2/index.php =404;
    

    Nginx will now try and process your request for /dev/[user]/www/[project]/something/else like this:

    First try /opt/dev/[user]/www/[project]/something/else/index.php

    Next try /opt/dev/[user]/www/[project]/something/else.php

    Next try and show the index of /opt/dev/[user]/www/[project]/something/else/

    Next try and fall back to /opt/dev/[user]/www/[project]/index.php In your PHP script the value of $_SERVER['HTTP_X_ROUTE'] will be '/something/else'

    Finally, if none of those work return 404.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题