Home > database >  How to get REQUEST_URI like "domen/folder" in nginx?
How to get REQUEST_URI like "domen/folder" in nginx?

Time:06-30

Please, help me with one question! I'he got project with structure like:

enter image description here

And index.php like

enter image description here

When i type in browser mysite/blablabla i got

enter image description here

but when I type "mysite/folder1" - browser just open this folder.

How can I get REUEST_URI like "mysite/folder1" or "mysite/folder2"

I use OpenServer (nginx). Sorry if I explain poorly, bad English.

This file: nginx.conf from C:/nginx

http {
include       mime.types;
default_type  application/octet-stream;
sendfile        off;
keepalive_timeout  65;


server {
    listen       80;
    server_name  onix-academy-php-framework;
    root   d:/dropbox/onix-academy-php-framework;
    index  index.php;
    #autoindex  on;
    
    location / {
       try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
       fastcgi_pass  127.0.0.1:9000;
       fastcgi_index index.php;
       fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
       include       fastcgi_params;
   }
}

This file: nginx.conf from OpenServer

server {
listen                    %ip%:%httpport%;
listen                    %ip%:%httpsport% ssl;
server_name               %host% %aliases%;
root                      '%hostdir%';
#autoindex                 on;
index                     index.php index.html index.htm;

ssl_certificate           '%sprogdir%/userdata/config/cert_files/server.crt';
ssl_certificate_key       '%sprogdir%/userdata/config/cert_files/server.key';
#ssl_trusted_certificate  '';

# Disable MIME sniffing
add_header X-Content-Type-Options 'nosniff' always;

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

    location ~ \.php$ {
        try_files      $fastcgi_script_name =404;
        fastcgi_pass   backend;
        include        '%sprogdir%/userdata/config/nginx_fastcgi_params.txt';
    }
    
    #location ^~ /Classes/ {
    #   return 302 /;
    #}
}

When I use "autoindex on" folders just open: enter image description here

And when I commented "#autoindex on" I get 403 forbidden: enter image description here

CodePudding user response:

You need to setup your nginx to send all incoming requests with a handoff to PHP.

Symfony has a simple setup documented. Although it is tailored to symfony, it can be used for standard PHP as well.

Try replacing the server part of your first config with this

server {
    listen       80;
    server_name  onix-academy-php-framework;
    root   d:/dropbox/onix-academy-php-framework;
    index  index.php;
    #autoindex  on;

    location / {
       try_files $uri /index.php$is_args$args;
    }

    location ~ ^/index\.php(/|$) {
       fastcgi_pass  127.0.0.1:9000;

       # not sure if this one is needed, try it with or without:
       fastcgi_split_path_info ^(. \.php)(/.*)$;

       fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
       fastcgi_param DOCUMENT_ROOT $realpath_root;
       include       fastcgi_params;
   }
}
  • Related