LevelRanks nginx configuration in caddy

Hi, please help adapting nginx to caddyfile

location / {
    try_files $uri $uri/ /index.php?$query_string;
    rewrite ^([^.]*[^/])$ $1/ permanent;
    rewrite !.(gif|jpg|png|ico|css|js|svg|js_controller.php)$ /index.php;
}

Project here

Thanks🙏

What did you try?

Please fill out the help topic template as per the forum rules.

Caddy and php running in docker:

{
    servers :443 {
        timeouts {
            read_body 120s
        }
    }
}
example.com {
    log {
        output file /var/log/caddy/example.com-access.log
        format console
    }
    root * /srv/example.com
    file_server
    encode zstd gzip
    php_fastcgi php74:9000

    respond /.ht* 403

    @dotFiles {
        path */.*
        not path /.well-known/*
    }


    handle /stats* {
        try_files {path} {path}/ /stats/index.php?{query}
        #rewrite ^([^.]*[^/])$ $1/ permanent;
        #rewrite !.(gif|jpg|png|ico|css|js|svg|js_controller.php)$ /index.php;
    }
}

Sorry, it’s very difficult for me to do this, because my level of English is very low

You don’t need this because php_fastcgi has try_files built-in.

What problem are you actually having? What’s not working? Show your logs, show example requests. Explain the problem.

Then at the address example.com/stats/adminpanel will issue 404

It takes a rule to have a slash here:
image
like this:

get /adminpanel:

2024/05/01 05:33:34.873 INFO    http.log.access.log3    handled request {"request": {"remote_ip": "XX.XX.XX.XX", "remote_port": "49595", "client_ip": "XX.XX.XX.XX", "proto": "HTTP/1.1", "method": "GET", "host": "example.com", "uri": "/stats/adminpanel", "headers": {"Connection": ["keep-alive"], "Accept-Encoding": ["gzip, deflate"], "Accept-Language": ["ru-RU,ru;q=0.9"], "Cookie": [], "Upgrade-Insecure-Requests": ["1"], "Dnt": ["1"], "User-Agent": ["Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36"], "Accept": ["text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7"], "Referer": ["http://example.com/stats/"]}}, "bytes_read": 0, "user_id": "", "duration": 0.037232391, "size": 4016, "status": 200, "resp_headers": {"Content-Encoding": ["gzip"], "Vary": ["Accept-Encoding"], "Server": ["Caddy"], "Pragma": ["no-cache"], "X-Powered-By": ["PHP/7.4.33"], "Content-Type": ["text/html; charset=utf-8"], "Expires": ["Thu, 19 Nov 1981 08:52:00 GMT"], "Cache-Control": ["no-store, no-cache, must-revalidate"]}}

2024/05/01 05:33:34.967 INFO    http.log.access.log3    handled request {"request": {"remote_ip": "XX.XX.XX.XX", "remote_port": "49595", "client_ip": "XX.XX.XX.XX", "proto": "HTTP/1.1", "method": "POST", "host": "example.com", "uri": "/stats/adminpanel", "headers": {"User-Agent": ["Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36"], "X-Requested-With": ["XMLHttpRequest"], "Referer": ["http://example.com/stats/adminpanel"], "Connection": ["keep-alive"], "Accept": ["*/*"], "Accept-Encoding": ["gzip, deflate"], "Cookie": [], "Content-Length": ["9"], "Dnt": ["1"], "Content-Type": ["application/x-www-form-urlencoded; charset=UTF-8"], "Origin": ["http://example.com"], "Accept-Language": ["ru-RU,ru;q=0.9"]}}, "bytes_read": 9, "user_id": "", "duration": 0.012116805, "size": 170, "status": 200, "resp_headers": {"Server": ["Caddy"], "Expires": ["Thu, 19 Nov 1981 08:52:00 GMT"], "Cache-Control": ["no-store, no-cache, must-revalidate"], "Pragma": ["no-cache"], "X-Powered-By": ["PHP/7.4.33"], "Content-Type": ["text/html; charset=utf-8"]}}

adminpanel/?section=general:

2024/05/01 05:33:59.295 INFO    http.log.access.log3    handled request {"request": {"remote_ip": "XX.XX.XX.XX", "remote_port": "49595", "client_ip": "XX.XX.XX.XX", "proto": "HTTP/1.1", "method": "GET", "host": "example.com", "uri": "/stats/adminpanel?section=general", "headers": {"Cookie": [], "Connection": ["keep-alive"], "Upgrade-Insecure-Requests": ["1"], "User-Agent": ["Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36"], "Accept": ["text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7"], "Accept-Encoding": ["gzip, deflate"], "Dnt": ["1"], "Referer": ["http://example.com/stats/adminpanel"], "Accept-Language": ["ru-RU,ru;q=0.9"]}}, "bytes_read": 0, "user_id": "", "duration": 0.043548586, "size": 102, "status": 200, "resp_headers": {"X-Powered-By": ["PHP/7.4.33"], "Content-Type": ["text/html; charset=utf-8"], "Expires": ["Thu, 19 Nov 1981 08:52:00 GMT"], "Server": ["Caddy"], "Cache-Control": ["no-store, no-cache, must-revalidate"], "Pragma": ["no-cache"]}}

2024/05/01 05:33:59.358 INFO    http.log.access.log3    handled request {"request": {"remote_ip": "XX.XX.XX.XX", "remote_port": "49595", "client_ip": "XX.XX.XX.XX", "proto": "HTTP/1.1", "method": "GET", "host": "example.com", "uri": "/stats/adminpanel?code=404&description=Oopss...", "headers": {"Dnt": ["1"], "Accept": ["text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7"], "Referer": ["http://example.com/stats/adminpanel?section=general"], "Connection": ["keep-alive"], "Upgrade-Insecure-Requests": ["1"], "User-Agent": ["Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36"], "Accept-Encoding": ["gzip, deflate"], "Accept-Language": ["ru-RU,ru;q=0.9"], "Cookie": []}}, "bytes_read": 0, "user_id": "", "duration": 0.007109694, "size": 420, "status": 200, "resp_headers": {"Cache-Control": ["no-store, no-cache, must-revalidate"], "Pragma": ["no-cache"], "Content-Encoding": ["gzip"], "Vary": ["Accept-Encoding"], "Server": ["Caddy"], "X-Powered-By": ["PHP/7.4.33"], "Content-Type": ["text/html; charset=utf-8"], "Expires": ["Thu, 19 Nov 1981 08:52:00 GMT"]}}

This log shows me that the request did make it to your PHP app, and your PHP app responded with status code 200. The problem isn’t with Caddy, your PHP app just isn’t doing what’s expected.

/stats/adminpanel?code=404&description=Oopss…

Namely, in order for it to work correctly, I need nginx rewrite rules, adapted for caddy

Or, your app should be fixed to not require the rewrite rules. I don’t see why they would be necessary.

Either way, I don’t have enough context here about how the app is meant to work, so I can’t reasonably help.