Solved My first site with only nginx is a disaster: not rewrite short urls

castris

Verified User
Joined
Apr 16, 2021
Messages
144
Location
Arcenillas
Hi

I wanted to try a migration from a server where everything I have is manually configured, with Nginx.

So in DirectAdmin, I configured only Nginx without Apache.

Surprise. Now I'm lost with the templates because I don't see how to apply the supposed template for WordPress.

I understand that the site (or so I think), doesn't have short URLs working because

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

And when I go to Dashboard > Custom HTTPD Configurations > View Domain Configuration > Customize configuration, I don't see how I can modify it.

What I would like the most is to know if it can be done in shell mode.

The issue of changing things in the panel dashboards drives me crazy. I understand it's necessary (sic) but I've always believed it should also be possible via shell.

Any help?
 
It is possible via shell, but why not to do it on user level, in nginx rewrites just select wordpress template?

Shell way (not the most convenient) can be global like this:

Or local per-domain (custom httpd configutation on admin level just saves a file with that content on a filesystem).

I’d still recommend just selecting wordpress template for url rewrite in user level :)
 
A lot of thanks.

Despite for me. I think is level admin question.

I see now, in user level, Nginx URL Writes... evo/user/nginx-templates

Best regards.
 
Ops.

I don't see error..

I have tired with / and /home/USER/domains/DOMAIN.tld/public_html

All cases, get same error with nginx server only.
 

Attachments

  • Error_nginx.jpg
    Error_nginx.jpg
    15.2 KB · Views: 8
  • Error_nginx_root_path.jpg
    Error_nginx_root_path.jpg
    19.1 KB · Views: 8
Hello and Thank you so much for your help @smtalk

As a general rule, I always check the logs in the shell.

In this case, since I wasn't even sure which log something like this would go in, I got an error telling me right away that there was no information to give me, so I activated tail -f for all logs, with 0 results that could tell me anything.


Bash:
2025/05/25 08:36:19 [error] 288558#0: *6611 "/home/castris/domains/castris.com/private_html/blog/index.php" is not found (2: No such file or directory), client: 91.121.226.50, server: castris.com, request: "GET /blog/ HTTP/2.0", host: "castris.com", referrer: "https://castris.com/"
2025/05/25 08:37:57 [error] 288559#0: *6624 "/home/castris/domains/castris.com/private_html/blog/index.php" is not found (2: No such file or directory), client: 91.121.226.50, server: castris.com, request: "GET /blog/ HTTP/2.0", host: "castris.com", referrer: "https://castris.com/"


That's all the log says.

And the worst part is that after giving me the error, it shows me that the job was done.

I've tried absolute, relative, etc. paths. It's good that this was also well documented. At least I wasn't able to see it in the documentation.

Error_nginx_root_path.jpg
Error_nginx.jpg

Unknow Error
Request failed with status code 404 and provided no explanation for why it failed


Also, if I go to the nginx configuration for the vhost, I see its configuration, although it's not correct, which is why the problem occurs.

The user sees it as cat /usr/local/directadmin/data/users/castris/nginx_php.conf

NGINX:
        # use fastcgi for all php files
        location ~ \.php$
        {
            try_files $uri =404;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            include /etc/nginx/fastcgi_params;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            if ( $skip_cache ~ "^$" ) {
                set $skip_cache 1;
            }
            fastcgi_cache_bypass $skip_cache;
            fastcgi_no_cache $skip_cache;
            fastcgi_cache FASTCGICACHE;
            fastcgi_cache_valid 60m;
            include /etc/nginx/nginx_limits.conf;
            if (-f $request_filename)
            {
                fastcgi_pass unix:/usr/local/php83/sockets/castris.sock;
            }
        }
        location ~ \.(php53|php54|php55|php56|php70|php71|php72|php73|php74|php80|php81|php82)$
        {
            deny all;
        }

But in the original file that directadmin produces after the error, and which doesn't work,

I made some changes to /usr/local/directadmin/data/users/castris/nginx.conf just to check that my idea was correct.

To temporarily leave it as it should be.


NGINX:

# Auto generated nginx config file by DirectAdmin version 1.677
# Modifying this file is not recommended as any changes you make will be
# overwritten when the user makes any changes to their website

# For global config changes that affect all Users, see this guide:
# http://help.directadmin.com/item.php?id=558
# For local config changes that only affect one User, see this guide:
# http://help.directadmin.com/item.php?id=3


server
{
    listen 5.135.93.116:80;
    server_name castris.com www.castris.com;
    access_log /var/log/nginx/domains/castris.com.log;
    access_log /var/log/nginx/domains/castris.com.bytes bytes;
    error_log /var/log/nginx/domains/castris.com.error.log;
    root "/home/castris/domains/castris.com/public_html";
    index index.php index.html index.htm;

    # WordPress specific rules
    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    # Add security headers
    add_header X-Content-Type-Options nosniff;
    add_header X-Frame-Options SAMEORIGIN;
    add_header X-XSS-Protection "1; mode=block";

    # Deny access to hidden files
    location ~ /\. {
        deny all;
        access_log off;
        log_not_found off;
    }

    # Deny access to uploads PHP files
    location ~* /(?:uploads|files)/.*\.php$ {
        deny all;
    }

    # Cache static files
    location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
        expires 365d;
        add_header Cache-Control "public, no-transform";
    }

    # use fastcgi for all php files
    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        include /etc/nginx/fastcgi_params;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        if ( $skip_cache ~ "^$" ) {
            set $skip_cache 1;
        }
        fastcgi_cache_bypass $skip_cache;
        fastcgi_no_cache $skip_cache;
        fastcgi_cache FASTCGICACHE;
        fastcgi_cache_valid 60m;
        include /etc/nginx/nginx_limits.conf;
        if (-f $request_filename) {
            fastcgi_pass unix:/usr/local/php83/sockets/castris.sock;
        }
    }
    location ~ \.(php53|php54|php55|php56|php70|php71|php72|php73|php74|php80|php81|php82)$
    {
        deny all;
    }
    # Mail auto configuration (Thunderbird)
    location = "/.well-known/autoconfig/mail/config-v1.1.xml" {
        proxy_pass http://unix:/usr/local/directadmin/shared/internal.sock;
        proxy_set_header X-Forwarded-For  $remote_addr;
        proxy_set_header X-Forwarded-Host $host;
    }
    include /etc/nginx/webapps.conf;
}

server
{
    listen 5.135.93.116:443 ssl;
    http2 on;
    server_name castris.com www.castris.com;
    access_log /var/log/nginx/domains/castris.com.log;
    access_log /var/log/nginx/domains/castris.com.bytes bytes;
    error_log /var/log/nginx/domains/castris.com.error.log;
    root "/home/castris/domains/castris.com/private_html";
    index index.php index.html index.htm;
    ssl_certificate /usr/local/directadmin/data/users/castris/domains/castris.com.cert.combined;
    ssl_certificate_key /usr/local/directadmin/data/users/castris/domains/castris.com.key;

    # WordPress specific rules
    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    # Add security headers
    add_header X-Content-Type-Options nosniff;
    add_header X-Frame-Options SAMEORIGIN;
    add_header X-XSS-Protection "1; mode=block";
    add_header Strict-Transport-Security "max-age=31536000" always;

    # Deny access to hidden files
    location ~ /\. {
        deny all;
        access_log off;
        log_not_found off;
    }

    # Deny access to uploads PHP files
    location ~* /(?:uploads|files)/.*\.php$ {
        deny all;
    }

    # Cache static files
    location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
        expires 365d;
        add_header Cache-Control "public, no-transform";
    }

    # use fastcgi for all php files
    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        include /etc/nginx/fastcgi_params;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        if ( $skip_cache ~ "^$" ) {
            set $skip_cache 1;
        }
        fastcgi_cache_bypass $skip_cache;
        fastcgi_no_cache $skip_cache;
        fastcgi_cache FASTCGICACHE;
        fastcgi_cache_valid 60m;
        include /etc/nginx/nginx_limits.conf;
        if (-f $request_filename) {
            fastcgi_pass unix:/usr/local/php83/sockets/castris.sock;
        }
    }
    location ~ \.(php53|php54|php55|php56|php70|php71|php72|php73|php74|php80|php81|php82)$
    {
        deny all;
    }
    # Mail auto configuration (Thunderbird)
    location = "/.well-known/autoconfig/mail/config-v1.1.xml" {
        proxy_pass http://unix:/usr/local/directadmin/shared/internal.sock;
        proxy_set_header X-Forwarded-For  $remote_addr;
        proxy_set_header X-Forwarded-Host $host;
    }
    # Automatic security.txt (RFC 9116)
    location = "/.well-known/security.txt" {
        proxy_pass http://unix:/usr/local/directadmin/shared/internal.sock;
        proxy_set_header X-Forwarded-For  $remote_addr;
        proxy_set_header X-Forwarded-Host $host;
    }
    include /etc/nginx/webapps.ssl.conf;
}

And indeed now everything works.
Just to see it, it was worth it.

So, I still don't understand why something as simple as applying a template becomes such a headache, an error that not even the frontend system can fix, and that the backend system (logs) gives me any information about.

In that scenario, the application, aka Directadmin, leaves me out of place, since I owe it to it and its rules.

Another question about the manual, and about WordPress. I didn't write down where I found it, whether in the Directadmin documentation or somewhere else, about fastcgi_cache_purge, a module that should be compiled in nginx if we want to work correctly with nginx purging in WordPress. Although this may be for another separate post.

I appreciate your help, and that of the people at Directadmin


Attention! The URL doesn't resolve to DirectAdmin, but rather to my vanilla machine, where it's been for years. Due to this issue, I've left the email in DirectAdmin, but I'm keeping the site on its original system. I'm testing by tuning the IP in /etc/hosts to access the DirectAdmin site.
 
Last edited:
Uff..

In the end, it was the simplest thing.

An error in the EVO process caused the issue.

Something failed in the first instance, and I started going around in the wrong direction.

Simple.

Path / and WordPress FastCGI, and that’s it. Just wait a bit and verify.
 
Back
Top