可以根据域地址后的第一段URL动态更改nginx'索引'

可以根据域地址后的第一段URL动态更改nginx'索引'

问题描述:

I have set root in nginx file: /var/www/html/public,

My domain is example.com.

I want to achieve that if I type example.com/first it will run /var/www/html/public/first.php file.

If I type example.com/second it will run /var/www/html/public/second.php.

If I type example.com/third it will run /var/www/html/public/third.php.

It is even possible?

我在nginx文件中设置了root: / var / www / html / public strong>, p>

我的域名是example.com。 p>

如果我输入 example.com/first strong>,我希望实现这一点 将运行 /var/www/html/public/first.php strong>文件。 p>

如果我输入 example.com/second strong>它 将运行 /var/www/html/public/second.php strong>。 p>

如果我输入 example.com/third strong>,它将会 运行 /var/www/html/public/third.php strong>。 p>

甚至可能吗? p> div>

@Richard Smith give me huge support by his advise. I search about extension-less php, and because not everyone know this (like me before) I paste my nginx config, which do exactly what i wanted to achieve.

server {
    listen 80;

    root /vagrant/public;

    # Add index.php to the list if you are using PHP
    index index.php index.html index.htm;

    server_name your_domain_name; #for example example.com

    location / {
         try_files $uri $uri/ @extensionless-php;
         index index.php index.html;
    }
    location @extensionless-php {
         rewrite ^(.*)$ $1.php last;
    }

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    # You must use this, because if You not use it, You will get surce code instead of result of php script execution.
    location ~ \.php(/|$)  {
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
}