12

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?

1
  • Usually nginx's config includes every file from /etc/nginx/sites-enabled/ and/or /etc/nginx/conf.d. Commented Feb 14, 2017 at 16:56

2 Answers 2

17

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.

Sign up to request clarification or add additional context in comments.

Comments

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'}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.