nginx phpstudy 怎么配置隐藏index.php?

windows系统 phpstudy集成环境,nginx服务, 安装laravel后除了首页可以访问,make:auth后的注册和登录页面都不能访问,网上查找原因发现只要在域名后面把index.php写上就行,然后知道是pathinfo 的问题,在nginx的vhosts.conf文件上按照文档的指导加了一行

try_files $uri $uri/ /index.php?$query_string;

这个是我的vhost.conf配置文件内容,除了上面一行,其它都没动:

server {
        listen       80;
        server_name  laravel.demo.com ;
        root   "E:/pms/laravel/public";
        location / {
            index  index.html index.htm index.php;
            #autoindex  on;
            try_files $uri $uri/ /index.php?$query_string;
        }
        location ~ \.php(.*)$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_split_path_info  ^((?U).+\.php)(/?.+)$;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            fastcgi_param  PATH_INFO  $fastcgi_path_info;
            fastcgi_param  PATH_TRANSLATED  $document_root$fastcgi_path_info;
            include        fastcgi_params;
        }
}

现在倒是地址自动添加index.php了,但是按照文档所说,加上try_files那一行是为了美化url, 但是反而是自动添加index.php了,怎么隐藏这个index.php?我网上查的,和配置文件默认的写法一样,求助大佬,看看是哪里的问题?

阅读 5.2k
1 个回答

我记得nginx.conf默认有几条基础的server规则,你把它都清掉,只留下include vhosts.conf试试~

附上我自己的配置:

nginx.conf

worker_processes 1;
error_log  logs/error.log;
pid        logs/nginx.pid;

events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile    on;
    tcp_nopush  on;
    keepalive_timeout 65;

    fastcgi_connect_timeout 300;
    fastcgi_send_timeout    300;
    fastcgi_read_timeout    300;
    fastcgi_buffer_size     128k;
    fastcgi_buffers 4 128k;
    fastcgi_busy_buffers_size 256k;
    fastcgi_temp_file_write_size 256k;

    gzip on;
    gzip_min_length  1k;
    gzip_buffers     4 32k;
    gzip_http_version 1.1;
    gzip_comp_level 2;
    gzip_types       text/plain application/x-javascript text/css application/xml;
    gzip_vary on;
    gzip_disable "MSIE [1-6].";

    server_names_hash_bucket_size 128;
    client_max_body_size     100m;
    client_header_buffer_size 256k;
    large_client_header_buffers 4 256k;

    include vhosts.conf;
}

vhosts.conf

server {
    listen       80;
    server_name  localhost;

    root    "X:/www";
    location / {
        index index.php;
        try_files $uri $uri/ /index.php?$args;
    }

    error_page 404              /404.html;
    error_page 500 502 503 504  /50x.html;

    location = /50x.html {
        root html;
    }

    location ~ \.php(.*)$  {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        include fastcgi_params;
        fastcgi_split_path_info  ^((?U).+\.php)(/?.+)$;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        fastcgi_param  PATH_INFO  $fastcgi_path_info;
        fastcgi_param  PATH_TRANSLATED  $document_root$fastcgi_path_info;
        try_files $uri =404;
    }

    location ~ /\.(ht|svn|git) {
        deny all;
    }
}

(其实基本没加什么,都是原来那些……)

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题