Skip to content
TheChatArchiveThe Chat Archive
o1-preview-2024-09-12· OpenAIAi2 WildChat-4.8M (AI2)

server { listen 80; servername cswars.com; root /var/www/html; index index.php…

  1. 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

  2. Assistant

    It looks like the issue stems from the try_files directive in your Nginx configuration. Specifically, the try_files directive is currently set to:

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

    Here's what this does:

    1. $uri: Nginx checks if the exact URI exists as a file. If it does, it serves that file.
    2. $uri/: If not, it checks if the URI corresponds to a directory (by appending a /). If it does, it tries to serve the default index file in that directory.
    3. /index.php?$query_string: If neither of the above exists, it passes the request to index.php with the original query string.

    In your case, when you request /adminpanel, Nginx checks:

    • Does /var/www/html/adminpanel exist 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 /adminpanel corresponds to an existing directory, Nginx doesn't proceed to pass the request to index.php as 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 the try_files directive to remove the $uri/ check:

    try_files $uri /index.php?$query_string;
    

    This tells Nginx:

    1. $uri: If the URI matches an existing file, serve it.
    2. /index.php?$query_string: Otherwise, pass the request to index.php with 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 the try_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 to index.php, allowing your application to handle routing internally.

    Note: After making this change, be sure to:

    1. Test the configuration: Run nginx -t to ensure there are no syntax errors.
    2. Reload Nginx: Run systemctl reload nginx or service nginx reload to apply the changes.

    Summary: Modify the try_files directive in your Nginx configuration to try_files $uri /index.php?$query_string; to ensure that requests to directories like /adminpanel are properly routed to index.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_files to try_files $uri /index.php?$query_string; so Nginx skips directories and passes to index.php