How To Create Custom Nginx Configuration Easily Using RunCloud

We are happy to bring easy custom Nginx configuration in RunCloud for you. You are able to create custom Nginx configs directly from the RunCloud dashboard.

Saving you much-needed time is another focus, as you can now add custom Nginx configs directly from your RunCloud dashboard instead of having to log into the linux terminal to add your custom Nginx config.

And you can test to run and debug your custom Nginx config from the RunCloud dashboard before you add the Nginx config, so no need to be afraid if your custom Nginx config can break your website.

What is Nginx config?

Nginx, pronounced like “engine-ex”, is the open source web server that powers more than 400 million websites. It is now also used for reverse proxying, caching, load balancing, media streaming, and more.

In this post, we will focus on Nginx as a web server. From Nginx success stories, we can see that Nginx open source web server has been used by many big companies, for example Adobe, Cloudflare, WordPress.com, ZenDesk, Groupon, etc.

Nginx is a high performance web server that is great for handling many concurrent connections and serving static content. All RunCloud servers are powered by Nginx web servers.

In RunCloud, Nginx web server can be configured from Nginx configuration files that are located in the /etc/nginx-rc/ directory, with The primary configuration file is /etc/nginx-rc/nginx.conf.

Each web application in RunCloud has their own Nginx configs that are located in the /etc/nginx-rc/conf.d/ directory.

And users can create custom Nginx configs that are located in the /etc/nginx-rc/extra.d/ directory.

There are three different web application stacks in RunCloud:

1) NGINX + Apache2 Hybrid Stack.

When using this stack, NGINX works as a reverse proxy for Apache2. Different from most setups, this stack doesn’t fully proxy Apache2. NGINX is really best at serving static content. By using this stack, your static files (eg: css, js, images, fonts) will be served by NGINX. If it is PHP, NGINX will pass it to Apache2 and the request will be passed to PHP-FPM.

This stack is great for average users that use .htaccess file. Custom Nginx config is needed for anything that can not be done from .htaccess file.

2) Native NGINX Stack.

When using this stack, your website will be fully controlled by NGINX. For PHP files, NGINX will pass it to FastCGI to communicate with PHP-FPM. This stack, of course, doesn’t support .htaccess file.

Custom Nginx config is needed if you are going to rewrite or extend Nginx by including your own config.

3) Native NGINX + Custom Config Stack

When using this stack, your website will be fully controlled by NGINX. This stack will not serve your PHP file.

This stack is suitable if you want to run Node.js / Python / Golang / WebSocket / Ruby on Rails / etc using RunCloud.

You can use custom Nginx configs to fully configure this stack.

Create a custom Nginx config

Previously, you can create custom Nginx configs in RunCloud manually using a Linux terminal by following instruction in our post on creating custom Nginx configurations.

With this new “Nginx config” feature, you can do it directly from the RunCloud dashboard easily. Please login to your RunCloud Dashboard, choose your server, go to Web Applications menu and click one of your web applications, and you will see the “Nginx Config” menu.

Click the “Create Config” button to start creating your custom Nginx config for current web application.

If you are familiar with Nginx config, you can choose “I want to write my own config” and start adding your config.

You can also start with a predefined Nginx config that we have provided. You can use it directly without customizing it.

For config “Type”, you can start with location.main (default) and location.main-before. You can start using other types when you are familiar with the Nginx config structure in RunCloud.

Run and debug custom Nginx config

Editing Nginx config directly using linux terminal could be dangerous and could make your website down if you do not have enough skill to debug your Nginx config issue.

RunCloud brings the “Run and Debug” feature that you can use to check if your custom Nginx config is okay or not.

If your custom Nginx config looks good, clicking “Run and Debug” will give you a green message.

If your custom Nginx config has some errors, clicking “Run and Debug” will give you red error message with the error details.

Even if you do not use the “Run & Debug” feature and try to click the “Add config” button directly, RunCloud will debug your custom Nginx config automatically and will stop when it has some errors.

So no need to be afraid if your custom Nginx config can break your website.

Predefined Nginx configs

We also provide some predefined Nginx configurations that you can use without customizing it since it is tailored according to your web application.

Redirect – from non-www to www

This simple Nginx config is useful if you want to redirect the non-www version of your website to www version.

Note: Please use this Nginx config if only you have added both non-www and www versions of your domain to your web application’s Domain Name menu, and you have set up DNS A Records for both non-www and www.

Redirect – from www to non-www

This simple Nginx config is useful if you want to redirect the www version of your website to non-www version.

Note: Please use this Nginx config if only you have added both non-www and www versions of your domain to your web application’s Domain Name menu, and you have se tup DNS A Records for both non-www and www.

Cloudflare – Restore Visitor IP

Cloudflare acts as a proxy to your RunCloud server, thus all visitors will appear from Cloudflare IP addresses. This is a hindrance to visitor tracking or identifying attackers.

In order to restore a visitor’s IP address, we need to retrieve the visitor’s originating IP address in the HTTP header from all Cloudflare’s IP addresses.

You can use this predefined Nginx config to restore your visitor IP address, if needed.

WordPress – 6G Firewall

The 6G Firewall is a powerful, well-optimized blacklist that checks all URI requests against a set of carefully constructed .htaccess directives, developed by Jeff Star from Perishable Press. This happens quietly behind the scenes at the server level, which is optimal for performance and resource conservation.

This predefined Nginx config brings 6G Firewall to Nginx web server in RunCloud servers.

WordPress – 7G Firewall

The 7G Firewall is the latest nG Firewall from Perishable Press. This predefined Nginx config brings 7G Firewall to Nginx web server in RunCloud servers.

Both 6G & 7G firewalls are easy-to-use, cost-effective ways to secure your site against malicious HTTP activity. It helps to protect against evil exploits, ill requests, and other nefarious garbage, such as XSS attacks, code injections, cache poisoning, response splitting, dual-header exploits, and more.

The 6G & 7G firewalls are good alternatives of our Web App Firewall (ModSecurity & OWASP CRS). You can try either 6G or 7G firewall if ModSecurity WAF doesn’t fit with your web application.

WordPress – Block xmlrpc.php

This simple Nginx config gives you an example to block xmlrpc.php file access in your website.

Developer Tips – RunCloud Nginx Config Structure

If you are a developer and want to explore Nginx config structure in RunCloud, this information could be useful for you.

When creating a custom Nginx config, we recommend you to use location.main or location.main-before type. It works for common cases, your Nginx config will be loaded in the main location block of your web application.

Configuration options in Nginx are called directives, and are organized into groups known as blocks.

You can learn more about blocks in Nginx configuration here.

location.http

The http block contains directives for handling web traffic. You can create custom Nginx configs that will be loaded in the http block, right before the server block of your web application.

location.main-before

The location block lets your configure how Nginx will respond to requests for resources within the server.

You can use location-main-before to load custom Nginx configs before the main location blocks in your web application.

location.root

You can use location.root to load custom Nginx configs inside the root location block that serves the document root of your website.

location.static

You can use location.static to load custom Nginx configs inside the static location block that serves static assets (css / js / images / fonts / etc) of your website.

location.html

You can use location.html to load custom Nginx configs inside the html location block that serves HTML pages in your website.

location.main

You can use use location.main to load custom Nginx config after the main location blocks in your web application.

headers

You can use headers to load custom Nginx config to extend headers output in your website.

Summary

At RunCloud, we are all about making your dev life easier, delivering a fast service, and ensuring your server is managed properly.

With this feature, you are able to create custom Nginx configs directly from the RunCloud dashboard.

All paid plan users (Basic, Pro, Business) can enjoy the full functionality of this feature.

This feature has been a requested feature that we knew would be useful to you. Never ever hesitate to suggest new features that you want to see, and we will make it happen.

Categories: Tutorials, Features, Server Management

Simplifying Server Management

RunCloud is a cloud server management tool that allows you to maintain full control of your server and host multiple WordPress, WooCommerce, Laravel, and PHP applications with fast and easy configuration.

Start Your Free Trial

5 days free trial no credit card required cancel anytime

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.