Home » Wordpress Installation » Nginx web server configuration

Nginx web server configuration

In this, the third part of the series about installing wordpress, we cover WordPress web server configuration. This is the file you need to create within the web server sites directory so that your new wordpress site appears on the Internet. The main two web servers in use at the moment are nginx and apache. I will cover the wordpress web server configuration for Nginx in this part and Apache will be covered in the next part.

This article will just cover http configuration – a later article will cover https configuration and how to generate certificates for an https site.

WordPress web server configuration for Nginx

If your preferred option for webserver is Nginx then this is the sort of option you need in order to serve up your new wordpress site. This was copied from the configuration of one of my own wordpress sites.

server {
        listen 80;
        listen [::]:80;
        client_max_body_size 5M;

        root /usr/local/sites/my-site-name.co.uk/wordpress;

        # Files to treat as index files
        index index.php index.html index.htm

        server_name my-site-name.co.uk www.my-site-name.co.uk;

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ /index.php$is_args$args;
                rewrite ^/sitemap(-+([a-zA-Z0-9_-]+))?\.xml$ "/index.php?xml_sitemap=params=$2" last;
                rewrite ^/sitemap(-+([a-zA-Z0-9_-]+))?\.xml\.gz$ "/index.php?xml_sitemap=params=$2;zip=true" last;
                rewrite ^/sitemap(-+([a-zA-Z0-9_-]+))?\.html$ "/index.php?xml_sitemap=params=$2;html=true" last;
                rewrite ^/sitemap(-+([a-zA-Z0-9_-]+))?\.html.gz$ "/index.php?xml_sitemap=params=$2;html=true;zip=true" last;

        location ~ \.php$ {
          include snippets/fastcgi-php.conf;
          fastcgi_pass unix:/run/php/php7.0-fpm.sock;
          fastcgi_read_timeout 1200;

        location /xmlrpc.php {
            deny all;

        location ~ /\.ht {
          deny all;

        location = /favicon.ico {
           log_not_found off;
           access_log off;

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

        location ~* \.(css|gif|ico|jpeg|jpg|js|png)$ {
            expires max;
            log_not_found off;


Obviously your file will need to be tailored to your particular site url and there may be other changes you need as well. In the section above there are some highlighted lines and I’ll explain what will or may need to change for each of those lines.

listen 80;

This is the instruction which tells Nginx the port to listen on. If this is the only virtual server configuration you have, then you will need to add the word default into the the clause i.e. listen 80 default_server; and you may want to do that if you want this site to be the default server, although bear in mind that there can only be one server set as default. Basically, default server means that if nginx receives a request for a server name it doesn’t understand it will serve the default server in place of the requested server. This is only likely if there is a problem with the domain DNS setting, but nginx requires that one of it’s virtual server configurations is marked as default. This would also apply to the ip6 entry i.e. listen [::]:80 default_server;

root /usr/local/sites/my-site-name.co.uk/wordpress; 

This is the root directory of your wordpress install and if you have been following the rest of this series your directory will be similar but the my-site-name.co.uk section will be different. Obviously if you have placed the wordpress files somewhere else your configuration will need to reflect this.

server_name my-site-name.co.uk www.my-site-name.co.uk;

In this section you need to place the domain name of your site. In this example I’ve added both the www and the non www version which you can do or you can stick to just one.

Section beginning location ~ .php$

This section tells nginx to pass control of any files which have the extension .php over to the PHP FPM handler (the FastCGI Process Manager). This is a php handler which runs within the web server process and therefore speeds up the execution of the php code. It is possible your version of nginx may not run this although not likely, but it could be you would need to alter this section to match the version of php you are running.

Section beginning location /xmlrpc.php

This section is denying access to anyone to the xmlrpc.php file which is a good security option if you don’t need access via xml-rpc. In most cases you won’t, but if you use the Jetpack module to monitor and administer your site then you will need to change this section. In a later article I will cover how I only give access to the WordPress.com network to limit the impact of random machines from around the world trying to gain access to your site via this file.

So, I’ve covered what goes in the file, but I haven’t explained where the file goes and how it is enabled. I’ll do that now.

The way Nginx is installed on many linux servers means it has a directory structure similar to apache in that there is a directory called sites-available and another directory called sites-enabled. This file is placed in the sites-available directory and then linked to the sites-enabled directory to allow nginx to use it.

I always call the file the same name as the domain I am creating because it keeps the whole system much easier to understand if the file is named the same as the domain. So, for the example file above, I would create a file called my-site-name.co.uk in the directory /etc/nginx/sites-available/and use the content as shown above. Since the directory will be owned by root you will need to use sudo nano /etc/nginx/sites-available/my-site-name.co.uk to do that.

Once the file is created it is enabled by using the following commands

sudo ln -s /etc/nginx/sites-available/my-site-name.co.uk /etc/nginx/sites-enabled/ 
sudo nginx -t
sudo service nginx reload

What these commands are doing is :

  • Setting up a soft link between the file in the sites-available directory to the sites-enabled (a soft link means the file essentially exists in both directories)
  • Running the nginx process with the -t or syntax test flag to make sure that there are no syntax errors
  • Reloading the server configuration so the new domain is enabled

It’s important that the second command is run and that it reports no errors. If there are errors in one file the whole web server will fail meaning any other sites you may be running will be down!

Once the server has reloaded it should be possible to run the WordPress install, but you probably need to configure the DNS (or trick your browser into thinking it is set up) before you can see it. I’ll cover that stage once I’ve covered setting a Virtual Server Configuration for Apache which is the next part of this series.



Leave a Reply

error: Content is protected !!
%d bloggers like this: