".htaccess" data to Nginx file

Discussion in 'Installation/Configuration' started by Emsanator, Dec 24, 2021.

Tags:
  1. Emsanator

    Emsanator Member

    I was using Apache and Nginx together before in CWP and I will start using ISPconfig from today, but I am having some problems. I would be glad if you can help with this.

    There are `.htaccess` files with different content in subdomains. I need to use it in the file, and the issue starts here.

    Here is a sample `.htaccess` file content that I used before: (for a subdomain: "panel.example.com")

    Code:
    RewriteEngine on
        RewriteBase /
        # If the request doesn't end in .php (Case insensitive) continue processing rules
        RewriteCond %{REQUEST_URI} !\.php$ [NC]
        # If the request doesn't end in a slash continue processing the rules
        RewriteCond %{REQUEST_URI} [^/]$
        # Rewrite the request with a .php extension. L means this is the 'Last' rule
        RewriteRule ^(.*)$ $1.php [L]
        # The following require certain allow overrides, if getting 500 error comment them out one by one
        # can be resolved in apache httpd.conf to ensure security alternatives
        ## If the request is for a valid directory
        RewriteCond %{REQUEST_FILENAME} -d [OR]
        ## If the request is for a valid file
        RewriteCond %{REQUEST_FILENAME} -f [OR]
        ## If the request is for a valid link
        RewriteCond %{REQUEST_FILENAME} -l
        RewriteCond %{REQUEST_URI} !.(css|gif|ico|jpg|js|png|swf|txt)$
        RewriteRule ^ - [L]
        RewriteRule ^([^/]+)/([^/]+)?$ index.php?cmd=$1&scd=$2 [L,QSA]
        RewriteRule ^([^/]+)/?$ index.php?cmd=$1 [L,QSA]
    
    I converted the above code to:

    Code:
    rewrite ^/([^/]+)/([^/]+)?$ /index.php?cmd=$1&scd=$2 last;
    rewrite ^/([^/]+)/?$ /index.php?cmd=$1 last;
    
    I will add the new code to the "example.com.vhost" file located in "/etc/nginx/sites-available/", but where exactly do I add it in conf. file?

    server {
    listen *:80;
    listen [::]:80;
    listen *:443 ssl http2;

    ssl_protocols TLSv1.3 TLSv1.2;
    listen [::]:443 ssl http2;
    ssl_certificate /var/www/clients/client4/web3/ssl/example.com-le.crt;
    ssl_certificate_key /var/www/clients/client4/web3/ssl/example.com-le.key;

    server_name example.com www.example.com panel.example.com assets.example.com storage.example.com help.example.com api.example.com;

    root /var/www/example.com/web/;
    disable_symlinks if_not_owner from=$document_root;

    if ($http_host = "panel.example.com") {
    rewrite ^(?!/(_SubDomains/Panel|stats|\.well-known/acme-challenge))/(.*)$ /_SubDomains/Panel/$2 last;

    ## HERE ?
    }

    if ($http_host = "assets.example.com") {
    rewrite ^(?!/(_SubDomains/Assets|stats|\.well-known/acme-challenge))/(.*)$ /_SubDomains/Assets/$2 last;
    }
    if ($http_host = "storage.example.com") {
    rewrite ^(?!/(_SubDomains/Storage|stats|\.well-known/acme-challenge))/(.*)$ /_SubDomains/Storage/$2 last;
    }
    if ($http_host = "help.example.com") {
    rewrite ^(?!/(_SubDomains/Help|stats|\.well-known/acme-challenge))/(.*)$ /_SubDomains/Help/$2 last;
    }
    if ($http_host = "api.example.com") {
    rewrite ^(?!/(_SubDomains/Api|stats|\.well-known/acme-challenge))/(.*)$ /_SubDomains/Api/$2 last;
    }

    index index.html index.htm index.php index.cgi index.pl index.xhtml standard_index.html;

    error_log /var/log/ispconfig/httpd/example.com/error.log;
    access_log /var/log/ispconfig/httpd/example.com/access.log combined;

    location ~ /\. {
    deny all;
    }

    location ^~ /.well-known/acme-challenge/ {
    access_log off;
    log_not_found off;
    auth_basic off;
    root /usr/local/ispconfig/interface/acme/;
    autoindex off;
    index index.html;
    try_files $uri $uri/ =404;
    }

    location = /favicon.ico {
    log_not_found off;
    access_log off;
    expires max;
    add_header Cache-Control "public, must-revalidate, proxy-revalidate";
    }

    location = /robots.txt {
    allow all;
    log_not_found off;
    access_log off;
    }

    location /stats/ {

    index index.html index.php;
    auth_basic "Members Only";
    auth_basic_user_file /var/www/clients/client4/web3/web//stats/.htpasswd_stats;
    add_header Content-Security-Policy "default-src * 'self' 'unsafe-inline' 'unsafe-eval' data:;";
    }

    location ^~ /awstats-icon {
    alias /usr/share/awstats/icon;
    }

    location ~ \.php$ {
    try_files /ba758c370926ab55427f2160db29ee1d.htm @php;
    }

    location @php {
    try_files $uri =404;
    include /etc/nginx/fastcgi_params;
    fastcgi_pass unix:/var/lib/php7.4-fpm/web3.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_intercept_errors on;
    }
    }

    Thank you.
     
    Last edited: Dec 24, 2021
  2. ahrasis

    ahrasis Well-Known Member HowtoForge Supporter

    In ISPConfig UI where in the domain website settings option you can add the required nginx directives.
     
  3. Emsanator

    Emsanator Member

    Hi,
    I am getting a 403 Error. I checked the file owner and permissions and I didn't see any errors. What could be the reason for this?

    upload_2021-12-24_16-23-10.png
     
  4. Emsanator

    Emsanator Member

    I noticed a mistake I made in regex. That's the correct Regex: ^([^/]+)/([^/]+)?$, No more 403 errors. But still, the problem persists. Rewrite still doesn't work, and when I remove the ".php" suffix at the end of the files, it downloads the PHP file to the computer. Log: PasteBin
     
  5. ahrasis

    ahrasis Well-Known Member HowtoForge Supporter

    It does not work like that but I could not explain in detail now as I am replying from my mobile. Try searching this forum for good working sample, basically something like location { }.
     

Share This Page