dongling2038
dongling2038
2018-08-25 15:19

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

  • nginx
  • php
  • sysadmin

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条回答