server { listen 80; servername cswars.com; root /var/www/html; index index.php…
- User
server { listen 80; server_name cswars.com;
root /var/www/html; index index.php index.html index.htm; charset utf-8; access_log /var/log/nginx/access.log; error_log /var/log/nginx/error.log; location / { # Check if the requested file exists as a regular file try_files $uri $uri/ /index.php?$query_string; } # PHP scripts handling location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/var/run/php/php7.4-fpm.sock; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } # Disable access to hidden files location ~ /\. { deny all; access_log off; log_not_found off; } autoindex off; # Uncomment and adjust as necessary to serve specific static types # location ~ \.(gif|jpg|png|ico|css|js|svg|woff2|mp3|mp4|webm|ttf)$ { # try_files $uri =404; # }}
172.71.172.234 - - [07/Oct/2024:17:31:31 +0000] "POST /adminpanel?section=servers HTTP/1.1" 200 101 "https://cswars.com/adminpanel?section=servers" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36 Edg/129.0.0.0"
it should redirect it to index.php but it not redirected as i can see
- Assistant
It looks like the issue stems from the
try_filesdirective in your Nginx configuration. Specifically, thetry_filesdirective is currently set to:try_files $uri $uri/ /index.php?$query_string;Here's what this does:
$uri: Nginx checks if the exact URI exists as a file. If it does, it serves that file.$uri/: If not, it checks if the URI corresponds to a directory (by appending a/). If it does, it tries to serve the defaultindexfile in that directory./index.php?$query_string: If neither of the above exists, it passes the request toindex.phpwith the original query string.
In your case, when you request
/adminpanel, Nginx checks:- Does
/var/www/html/adminpanelexist as a file? Probably not. - Does
/var/www/html/adminpanel/exist as a directory? If it does, Nginx tries to serve the directory's index file (e.g.,index.html,index.php, etc.), or might return a 403 error if directory listing is disabled.
Problem:
Since
/adminpanelcorresponds to an existing directory, Nginx doesn't proceed to pass the request toindex.phpas you'd expect. Instead, it tries to serve the directory directly.Solution:
To ensure that all requests are properly routed to
index.php(unless they correspond to actual files), you should modify thetry_filesdirective to remove the$uri/check:try_files $uri /index.php?$query_string;This tells Nginx:
$uri: If the URI matches an existing file, serve it./index.php?$query_string: Otherwise, pass the request toindex.phpwith the query string.
Updated Configuration:
server { listen 80; server_name cswars.com; root /var/www/html; index index.php index.html index.htm; charset utf-8; access_log /var/log/nginx/access.log; error_log /var/log/nginx/error.log; location / { try_files $uri /index.php?$query_string; } # PHP scripts handling location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/var/run/php/php7.4-fpm.sock; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } # Disable access to hidden files location ~ /\. { deny all; access_log off; log_not_found off; } autoindex off; }Explanation:
By removing the
$uri/from thetry_files, Nginx will no longer try to serve directories directly. Instead, if the request doesn't match an existing file, it will pass the request toindex.php, allowing your application to handle routing internally.Note: After making this change, be sure to:
- Test the configuration: Run
nginx -tto ensure there are no syntax errors. - Reload Nginx: Run
systemctl reload nginxorservice nginx reloadto apply the changes.
Summary: Modify the
try_filesdirective in your Nginx configuration totry_files $uri /index.php?$query_string;to ensure that requests to directories like/adminpanelare properly routed toindex.php.Answer:
Because Nginx finds a directory at "/adminpanel", it doesn’t redirect to index.php. To fix this, in your Nginx config, change
try_filestotry_files $uri /index.php?$query_string;so Nginx skips directories and passes to index.php