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 /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;
# }
}
оно должно перекидывать каждый запрос на /var/www/html/index.php, кроме js_controller.php который может лежать в любой папке, и других файлов по типу Js,css и т.д, но он почему то не перекинул, либо не передал параметры я хуй знает, мне просто 404 выдает сайт когда я захожу на https://cswars.com/adminpanel?section=servers
Assistant
It seems you're encountering a 404 error when trying to access https://cswars.com/adminpanel?section=servers, and you'd like all requests to be directed to /var/www/html/index.php, except for js_controller.php (which can be in any folder) and static files like JS, CSS, etc.
The issue likely stems from how Nginx is handling PHP files and the order of your location blocks. Here's how you can adjust your Nginx configuration to achieve the desired behavior:
Serve Static Files Directly: Ensure that static assets like .js, .css, images, etc., are served directly if they exist.
Handle js_controller.php in Any Directory: Create a location block that matches js_controller.php in any folder and processes it via PHP-FPM.
Route All Other Requests to index.php: Any other request should be directed to index.php, preserving query parameters.
Static Files Block: The location block with the regex \.(js|css|png|jpg|jpeg|gif|ico|svg|woff2|ttf|mp3|mp4|webm)$ ensures that static files are served directly if they exist.
js_controller.php Handling: The location block for js_controller.php uses a regex to match this file in any directory and specifies that these requests should be processed by PHP-FPM.
PHP Scripts Handling: Simplified this block by removing redundant fastcgi_param entries since snippets/fastcgi-php.conf includes the necessary configurations.
Main location / Block: Adjusted the try_files directive to first check for a file or directory and then route to index.php with the original query string if the file isn't found.
Steps to Apply the Changes:
Edit the Nginx Configuration File: Replace your current server block with the updated configuration shown above. Typically, this file is located at /etc/nginx/sites-available/default or a file specific to your domain within /etc/nginx/sites-available/.
Test the Nginx Configuration: Before reloading Nginx, test the configuration for syntax errors with:
bash
sudo nginx -t
Reload Nginx: If the test passes, reload Nginx to apply the changes:
bash
sudo systemctl reload nginx
Verify PHP-FPM Socket: Make sure that PHP-FPM is running and that the socket path (/var/run/php/php7.4-fpm.sock) is correct. Adjust the socket path if you're using a different PHP version.
Check File Permissions: Ensure that index.php and other necessary files in /var/www/html have the correct permissions and ownership so that Nginx and PHP-FPM can access them.
Review Error Logs: If you're still encountering issues, check Nginx and PHP-FPM error logs for detailed error messages:
Query String Preservation: The try_files directive in the main location block includes ?$query_string to ensure that query parameters are preserved when redirecting to index.php.
Ordering of Location Blocks: Nginx processes location blocks in a specific order. More specific regex patterns (like the one for js_controller.php) should come before more general patterns to prevent them from being overridden.
Avoiding Conflicts: By simplifying the PHP handling block and ensuring it doesn't conflict with other blocks, we prevent issues where PHP files aren't processed correctly.
Testing the Changes:
After applying the changes, test your website:
Visit https://cswars.com/adminpanel?section=servers and verify that you no longer receive a 404 error and that the request is correctly handled by index.php.
Access a known static file (e.g., https://cswars.com/css/style.css) to confirm that static files are being served correctly.
Navigate to a js_controller.php file located in a subdirectory to ensure it's being processed by PHP-FPM.
Conclusion:
By adjusting the Nginx configuration as shown, your server should now properly:
Serve static files directly.
Process js_controller.php files in any directory through PHP-FPM.
Redirect all other requests to index.php while preserving query parameters.
If you run into further issues, please provide specific error messages from your logs, and I'd be happy to help you troubleshoot further.