BROWSER CACHING Problem in NGINX

Hi!

When I try to paste the following code,

location ~* \.(txt|xml|js)$ {
    expires 8d;
}

location ~* \.(css)$ {
    expires 8d;
}

location ~* \.(flv|ico|pdf|avi|mov|ppt|doc|mp3|wmv|wav|mp4|m4v|ogg|webm|aac)$ {
    expires 8d;
}

location ~* \.(jpg|jpeg|png|gif|swf|webp)$ {
    expires 8d;
}

just above http section in nginx.conf file it gives following error

nginx: [emerg] "location" directive is not allowed here in /etc/nginx/nginx.conf:16
nginx: configuration file /etc/nginx/nginx.conf test failed

  • Sajid
    • DEV MAN’s Sidekick

    Hi @rawalbaig,
    Hope you are doing good today :slight_smile:

    Its because you are adding it in http directive. Add them in location or server directives and then check again. Location directive can be used inside server and location directives only. You have it inside http directive, which is not allowed.

    Please make that change and let us know how it goes. Also, if the issue exist please grant support staff access so I can take a closer look. You can grant access from WPMU DEV > Support > Support Access > Grant Access or see this manual.

    Take care and have a nice day :slight_smile:
    Cheers, Sajid

      • Mensmaximus
        • Site Builder, Child of Zeus

        I have no clue about Plesk or any other hosting control panel. So I only can tell you where I put those things native.

        On my linux servers Ngnix lives under /etc/nginx. Within that directory I have created three additional directories called 'global', 'sites.enabled' and 'site.disbaled'. In the 'global' directory you will find couple of configuration files to be used in a modular way.

        Lets take a look at one of them: header.conf

        expires 7d;
        add_header Pragma public;
        add_header Cache-Control "max-age=604800, public, must-revalidate, proxy-revalidate";
        add_header Content-Security-Policy "default-src 'self' 'unsafe-inline' 'unsafe-eval' https: data: http:;";
        add_header Strict-Transport-Security "max-age=63072000; includeSubdomains; preload";
        add_header X-Content-Type-Options nosniff;
        add_header X-XSS-Protection "1; mode=block";
        
        location ~* ^.+\.(ogg|ogv|eot|otf|woff|woff2|mp4|ttf|rss|atom|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$ {
            access_log off;
            log_not_found off;
        }
        
        location = /favicon.ico {
            log_not_found off;
            access_log off;
        }
        
        location = /robots.txt {
            log_not_found off;
            access_log off;
        }

        Now in my 'sites.enabled' directory you will find all the configuration files for the domains hosted on the server. Within such a server configuration I can include the header.conf file from above:

        server {
            listen       192.168.20.44:80;
            server_name  my.local;
            root /var/www/my.local/htdocs;
        
            include global/header.conf;
        
            location / {
                index  index.php;
                try_files $uri $uri/ /index.php?$args;
                include global/wordpress-multi.conf;
                include global/restrict.conf;
                include global/php.conf;
            }
        }

        I like this modular apache-like approach as it keeps the configurations small, clean and simple.

        Hope this helps

          • Mensmaximus
            • Site Builder, Child of Zeus

            Usually Nginx does not know about .htaccess ot user.ini or any other configuration loaded during run time. The main advantage of Nginx is speed gained from loading any configuration during start up. I am sure there are options in Plesk to add your own directives to a host file (domain, virtual server or whatever it might be called in Plesk), however I have never used it and don't know anything about Plesk.

  • Rawal
    • WPMU DEV Initiate

    Hello Nasita!

    Thanks for getting back to me :slight_smile:

    This issue is still there and not resolved because I don't know much about working in a VPS. I have just installed Plesk Panel and Install WP in it.

    Any help will be much appreciated.

    • Mensmaximus
      • Site Builder, Child of Zeus

      Rawal I ran an ISP for 15 years and have seen many people ordering VPS with only little knowledge about linux, apache/nginx, php, mysql and security. Many of them struggled and had to learn the burden and time to manage a VPS was simply to much for them. If this VPS and WordPress install is not just for learning and especially if you host costumers on it you might think about a managed hosting account.

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.