Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nginx how to add extra server configuration to without modifying nginx.conf

I want to add this extra config to my nginx.conf:

server {
  listen 0.0.0.0:8081;
  rewrite     ^  https://$host$request_uri? redirect;
}

But as my app is deployed in a hosting service I don't want to modify the already present nginx.conf. It can be problematic.

Is there any way I can add this extra configuration without modifying nginx.conf?

like image 623
fguillen Avatar asked Feb 14 '17 16:02

fguillen


People also ask

Can Nginx have multiple servers?

If you are using a dedicated server / VPS and want to host multiple applications on a separate domain and a single server then you will need to host multiple websites on a single server. You can achieve this with Apache / Nginx virtual hosting. Virtual hosting allows you to use a single VPS to host all your domains.

What method could you use to make dynamic configuration changes with Nginx plus?

NGINX Plus adds additional methods for modifying configuration dynamically. By using DNS as the definitive repository for upstream servers, you can make changes without touching NGINX Plus configuration files.

Do I need to restart Nginx after config change?

You need to reload or restart Nginx whenever you make changes to its configuration. The reload command loads the new configuration, starts new worker processes with the new configuration, and gracefully shuts down old worker processes.


1 Answers

There is no way you can add extra server configuration without modifying nginx.conf first. But good news is that you will have to modify nginx.conf only for once.

Just add this line in your nginx.conf

include /etc/nginx/config.d/*.conf;

You can name directory and path as per your choice. create directory and save your extra configuration in that as extra.conf with .conf extension. Any files you save with .confextension in this directory /etc/nginx/config.d will be automatically added to your nginx.conf.

You can even save multiple configurations like extra1.conf and extra2.conf for different uses and you can delete one without affecting other.

like image 198
Nipun Arora Avatar answered Oct 24 '22 03:10

Nipun Arora