Unable to stop nginx .conf files from being rebuilt frequently
Hello,
I'm having an issue with cPanel and nginx. Months ago, I installed nginx on my server and got it working. I recently added a new domain and had to make several changes to the file in /etc/nginx/conf.d/users/{username}.conf (username is a placeholder for my username). This file contains the server {} blocks for each individual domain I own.
I am able to make changes to this file, which gets nginx correctly pointing to my website (I am using Docker containers for Apache, Node.js, etc., which is why I need to change values in this .conf file), but the files seem to be getting rebuilt every few days. After 3-4 days, I revisit my site to find a 500 internal server error. I check the file and all of the directives I added are gone. The same thing happens when I manually rebuild the nginx configuration through the "NGINX Manager" interface on WHM.
I looked at the cPanel docs and found that I can add custom configuration for a specific server block by creating a .conf file in a specific directory:
"To customize a specific server block for a specific domain, create your .conf
file in the following directory:
/etc/nginx/conf.d/users/{username}/{domainname}/"
I created a .conf file in that directory (I replaced the username/domainname placeholders with my respective info) and copy/pasted the server block from the /etc/nginx/conf.d/users/{username}.conf to this new file (/etc/nginx/conf.d/users/username/domainname/custom_config.conf).
After doing this, I got a list of the same error messages after running "nginx -t", which says, "nginx: [warn] conflicting server name [url info], on 0.0.0.0:80, ignored". This is because now both .conf files have the same server_name directive.
I then deleted the server block from the first .conf file, etc/nginx/conf.d/users/{username}.conf, and restarted nginx. This worked! It's up and running at this point.
My issue is that this original configuration file that I deleted the server block from keeps being rebuilt. And when it is rebuilt, I am unable to access the website because of this conflicting server name error. How can I prevent this file from being rebuilt, or how can I add my custom configuration files correctly so that the server block I add overrides the initial server block or something like that? The way things are right now, I can't see how I can possibly ever add my own directives to any .conf file because it keeps being rebuilt. I will have to Dockerize the nginx container, which will have its unique set of issues, so I would prefer to just fix this rebuilding issue.
NOTE:
In case this solution will be mentioned, I also read under the "Global configuration" section of the documentation that you can
"Place any global .conf
files that you create in the /etc/nginx/conf.d/
directory.
If you want to adjust every server block on your server, create your .conf
file in the /etc/nginx/conf.d/server-includes/
directory."
I tried this and got an error about being unable to use a server block in that location:
"nginx: [emerg] "server" directive is not allowed here in /etc/nginx/conf.d/server-includes/custom_config.conf:8"
So this didn't work for me.
-
Hey there! There is no way to keep that file from rebuilding as that is a core function of cPanel.
The include system is in addition to the main configuration file, not in place of it. Can you add only the portions you want to change into the custom .conf file you've created?
0 -
Hey! Thanks for the response. I could try doing that but I don't know how I would be able to only add specific portions if I get an error when I repeat the server_name directive in my custom file.
0 -
Can you let me know the exactly values/variables you're trying to adjust in that configuration file?
0 -
Of course. In the /etc/nginx/conf.d/users/{username}.conf file, I have a "location / { ... }" block with several nginx directives.
In this block, I only want to change the "proxy_pass" value from what it currently is, which currently just points to my root domain's IP address, but I need it to point to an Docker container.
I also need to add a location block for socket.io ("location /socket.io/ { ... }"). So I add this to the end of the server block for the domain I need it for.
The rebuilding of this .conf file removes these directives.
Am I able to work around this by including a .conf file?
0 -
Thanks for the additional details. I spoke with some other technicians and they agreed that you should be able to create that without the "server_name" variable so you don't run into that conflict. Does that not work for you?
0 -
Do you mean I should be able to create a separate .conf file that I include using "include {file path}" to make these changes, and I would just exclude the server_name directive in that file? Or are you saying I should be able to make these changes directly in the .conf file I'm referencing?
0 -
You would still want to use the custom file of either
/etc/nginx/conf.d/users/USER/include.conf
for all domains under the user, or
/etc/nginx/conf.d/users/USER/domain.tld/include.conf
for the specific domain in question.
0 -
Okay. It's starting to come together, but I'm not sure how to format the additional file correctly.
In the main file that keeps getting rebuilt, /etc/nginx/conf.d/users/{username}.conf, I already have the directive
location / {
proxy_pass {IP address}
... (several other directives)
}In the second file, I add this same directive because I want to override the proxy_pass value:
location / {
proxy_pass {Docker container info}
}When I run "nginx -t", I get the error, "nginx: [emerg] duplicate location "/" in /etc/nginx/conf.d/users/{username}/{domain name folder}/custom_config.conf:8", as if I'm unable to specify the "location /" block as it is repeated.
Is there a way to get around this error? How do I establish that I want to connect to this Docker container specifically when the user visits the root of the site?
Regarding the second location block, "location /socket.io/", which I also added to my custom config file, that doesn't seem to be giving any errors, so that might actually be set up to work.
0 -
Thanks for the additional details - you can't override / like that, so I'm wondering if something like this would be better for your needs:
That would let you send traffic to the location of your choice without the need for customization to Nginx, and may result in a simpler configuration overall.
0 -
Okay. Thanks for the help.
0
Please sign in to leave a comment.
Comments
10 comments