8

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?

fguillen
  • 36,125
  • 23
  • 149
  • 210

2 Answers2

13

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.

Nipun Arora
  • 166
  • 5
0

There is one way, but you need to insert some changes to nginx.conf

you can create a template file extra_config that contains

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

and in nginx.conf add this string

{% include '%path_to_template_file%/extra_config'}
Alex Pol
  • 42
  • 5