How To Add A Custom Configuration To nginx-proxy

No Comments
Published: 04.09.2022

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.


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:

Server icon

VPS Hosting Course

Learn everything you need to know about servers and hosting your own applications!
    container_name: nginx
    image: nginxproxy/nginx-proxy
    restart: unless-stopped
        - 80:80
        - 443:443
        - /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
            max-size: "10m"
            max-file: "3"

For the WordPress Instance, the VIRTUAL_HOST environment variable is set to (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 they will be automatically redirected to 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;

upstream {
	## Can be connected with "docker_default" network
	# website
	# Fallback entry
	server down;

server {
	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;
	location / {
		return 301 https://www.$host$request_uri;

server {
	listen 443 ssl http2 ;
	ssl_session_timeout 5m;
	ssl_session_cache shared:SSL:50m;
	ssl_session_tickets off;
	ssl_certificate /etc/nginx/certs/;
	ssl_certificate_key /etc/nginx/certs/;
	ssl_dhparam /etc/nginx/certs/;
	ssl_stapling on;
	ssl_stapling_verify on;
	ssl_trusted_certificate /etc/nginx/certs/;
	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!

Need help or want to share feedback? Join my discord community!

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

After these steps, you should see that you are automatically redirected to! 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 or in the chat on the bottom right.


If this guide is helpful to you and you like what I do, please support me with a coffee!


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 *