简体   繁体   中英

".htaccess" data to Nginx file migration

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?

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

Yes you can add you rewrite directives in that place which you marked in config.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM