doulei3488 2017-05-22 13:34
浏览 16
已采纳

可以根据域地址后的第一段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?

  • 写回答

1条回答 默认 最新

  • dongliaoqiang8524 2017-05-22 15:01
    关注

    @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;
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序