How To Add A Custom Configuration To nginx-proxy

No Comments

Do you want to add a custom NGINX configuration to your nginx-proxy setup? In this post, you will learn how to do exactly that. For that will create a www redirect for a WordPress instance using nginx-proxy as an example.

Basics

As a foundation for this guide, we will use the WordPress setup that I explained in this post here. It consists of a database, WordPress itself, the Let’s Encrypt companion, and nginx-proxy. You can see the latter in the following snippet:

nginx:
    container_name: nginx
    image: nginxproxy/nginx-proxy
    restart: unless-stopped
    ports:
        - 80:80
        - 443:443
    volumes:
        - /var/run/docker.sock:/tmp/docker.sock:ro
        - /var/docker/nginx/html:/usr/share/nginx/html
        - /var/docker/nginx/certs:/etc/nginx/certs
        - /var/docker/nginx/vhost:/etc/nginx/vhost.d
        - /var/docker/nginx/conf:/etc/nginx/conf.d
    logging:
        options:
            max-size: "10m"
            max-file: "3"

For the WordPress Instance, the VIRTUAL_HOST environment variable is set to website.com (you have to change this to your own domain). For adding a custom configuration you have to create a file with the .conf ending inside the /etc/nginx/conf.d directory. In the nginx-proxy container configuration above you can find the directory in your local file system under /var/docker/nginx/conf.

Add a custom configuration for WordPress to nginx-proxy

To learn how to add a custom configuration we will create a simple www redirect for the WordPress instance created before. This means that when a visitor visits the domain website.com they will be automatically redirected to www.website.com. This practice was suggested by some posts on SEO and thus I made them for my website as well.

As explained in the section before we will have to create a new file called redirect.conf inside of /etc/nginx/conf.d or if mapped to this path inside of /var/docker/nginx/conf. The file itself follows the NGINX rules. You can get a simple overview here.

client_max_body_size 12M;

# website.com
upstream website.com-upstream {
	## Can be connected with "docker_default" network
	# website
	server 172.21.0.2:3000;
	# Fallback entry
	server 127.0.0.1 down;
}

server {
	server_name website.com;
	listen 80 ;
	# Do not HTTPS redirect Let'sEncrypt ACME challenge
	location ^~ /.well-known/acme-challenge/ {
		auth_basic off;
		auth_request off;
		allow all;
		root /usr/share/nginx/html;
		try_files $uri =404;
		break;
	}
	
	location / {
		return 301 https://www.$host$request_uri;
	}
}

server {
	server_name website.com;
	listen 443 ssl http2 ;
	ssl_session_timeout 5m;
	ssl_session_cache shared:SSL:50m;
	ssl_session_tickets off;
	ssl_certificate /etc/nginx/certs/website.com.crt;
	ssl_certificate_key /etc/nginx/certs/website.com.key;
	ssl_dhparam /etc/nginx/certs/website.com.dhparam.pem;
	ssl_stapling on;
	ssl_stapling_verify on;
	ssl_trusted_certificate /etc/nginx/certs/website.com.chain.pem;
	add_header Strict-Transport-Security "max-age=31536000" always;
	location / {
		return 301 https://www.$host$request_uri;
	}
}

The first section makes your container available, the second section allows for the validation of Let’s Encrypt certificates and the last section does the actual redirect to www. For other use cases, you have to adapt the third section to your needs, but in the first section, you should only update the domain to your needs!

In addition to creating the file, you also have to change the VIRTUAL_HOST environment variable of your WordPress Instance to www.website.com, rebuild the WordPress Container, and restart the nginx-proxy container!

After these steps, you should see that you are automatically redirected to www.website.com! In case it is not working immediately try in incognito mode, or with another browser. If after that it is still not working feel free to ask me through mail@programonaut.com or in the chat on the bottom right.

Conclusion

With this, we added a custom nginx configuration to nginx-proxy. The important bits to remember are to place the file inside of /etc/nginx/conf.d, to give the file a .conf ending, and to keep boilerplate like making the container available or allowing Let’s Encrypt “untouched”.

I hope this guide helped you in your efforts of configuring your server! In case you liked this post consider subscribing to my newsletter to get monthly updates on new content!

Discussion (0)

Add Comment

Your email address will not be published. Required fields are marked *