Question

Webserver Nginx download the index file when I want to go to the first page

Posted September 26, 2020 167 views
NginxWordPress

this is my Nginx config file

    ############# DOMAIN domainname.COM ####################
    server {
    server_name  domainname.com;
    rewrite ^(.*) http://www.domainname.com$1 permanent;
    return 301 $scheme://domainname.name$request_uri;
    }
    server {
    # Basic Domain
    server_name www.domainname.com;
    root /home/www/domainname;
    access_log  /var/log/nginx-domainname-access.log;
    error_log   /var/log/nginx-domainname-error.log;
    error_page 500 502 503 504 /500.html;
    client_max_body_size 4G;
    keepalive_timeout 10;
    # rewrite ^(.+)/index.html$ $1 permanent;
    ## REDIRECT domainname.com/index(.php) to domainname.com/
    if ($request_uri ~ ^(.*/)index(?:\.php)?$) {
    return 301 $1;
    }
    # OR USE :
    ## REDIRECT domainname.com/index(.html) to domainname.com/
    if ($request_uri ~ ^(.*/)index(?:\.html)?$) {
    return 301 $1;
    }
    ## REDIRECT domainname.com/index.html to domainname.com/index
    #if ($request_uri ~ ^(.+)\.html$) {
    #    return 301  $1;
    #}

    # To remove a slash at the end:
    rewrite ^/(.*)/$ /$1 permanent;

    #try_files $uri $uri/index.html =404;
    #try_files $uri $uri/ /index.php?q=$uri&$args;
    error_page 404 /404.html;

    location / {
    # URLs to attempt, including pretty ones.
    try_files   $uri $uri/ /index.php?$query_string;
    index index.html index.php index.py;
    autoindex on;
    }
    # Remove trailing slash to please routing system.
    if (!-d $request_filename) {
        rewrite     ^/(.+)/$ /$1 permanent;
    }
 # cache Media: images, icons, video, audio, HTC
  location ~* \.(jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm|htc)$ {
  expires 30d;
  add_header Cache-Control "public";
  }
  # cache.appcache, your document html and data
  location ~* \.(manifest|appcache|html?|xml|json)$ {
  expires -1;
  # access_log logs/static.log; # I don't usually include a static log
  }
  location ~* \.(css|js)$ {
   expires 7d;
  }
  # Feed
  location ~* \.(rss|atom)$ {
  expires 1h;
  add_header Cache-Control "public";
  }                                                  
  }

server {
   server_name ~^(www\.)(?<subdomain>.+).domainname.com$ ;
   root /home/www/domainname/$subdomain;
}
server {
   server_name ~^(?<subdomain>.+).domainname.com$ ;
   root /home/www/domainname/$subdomain;
}

But Nginx download the index file when I want to go to the first page

then I want to know about “location /” code segment

and I want to know why this occurred

Meanwhile, I had a 403 Forbidden error message before

My used Os is WordPress

please see the code and write me the correct one

I installed WHMCS on a subfolder and I want to know about that

please edit the code to get access to my installed WHMCS on the subfolder too.

thanks ahead and much appreciated

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
1 answer

Hello,

First, I can see you have a lot of redirects which might be causing the said issue :

    ## REDIRECT domainname.com/index(.php) to domainname.com/
    if ($request_uri ~ ^(.*/)index(?:\.php)?$) {
        return 301 $1;
    }
    # OR USE :
    ## REDIRECT domainname.com/index(.html) to domainname.com/
    if ($request_uri ~ ^(.*/)index(?:\.html)?$) {
        return 301 $1;
    }
    ## REDIRECT domainname.com/index.html to domainname.com/index
    #if ($request_uri ~ ^(.+)\.html$) {
    #    return 301  $1;
    #}

    # To remove a slash at the end:
    rewrite ^/(.*)/$ /$1 permanent;

Please try removing them, restarting Nginx to see if this would work properly without them.

As for your location, block, I can say it’s looking alright :

    location / {
        # URLs to attempt, including pretty ones.
        try_files   $uri $uri/ /index.php?$query_string;
        index index.html index.php index.py;
        autoindex on;
    }

Now, if you want to serve another location block for your WHMCS installation, you should be able to type in

    location /whmcs {
        # URLs to attempt, including pretty ones.
        try_files   $uri $uri/ /index.php?$query_string;
    }

Please note, you should put location /PATH/TO/FOLDER

The path to the folder should be based on your Document root. Example :

Your DocumentRoot is /var/www/html/domain.com. If you want to create a location block for a folder located in /var/www/html/domain.com/one/WHMCS, the location block should start the following way:

location /one/whmcs
Submit an Answer