I'm struggling to figure out the meaning of each nginx configuration file located in different directories. So far I've found these directories/files related:
/etc/nginx/nginx.conf - this is the main file that nginx will read. It also has several include directives to reference configurations defined in several other files.
/etc/nginx/conf.d/*.conf - in my default installation, there's one default.conf file at this location. Is this the one that I should go to edit if I want to, for example, add my own website?
/etc/nginx/sites-available - Sometimes there's a file named 'default' here. What is this directory for and what's this configuration file? What's its relationship with the default.conf file under conf.d?
/etc/nginx/sites-enabled - Another confusion. What's this directory then? And on my installation I see a symbolic link file pointing to the 'default' file in the sites-available directory. What's the point of doing this, separating into 2 directories but link them by some means?
I have the following config files and locations:
etc/ngnix/nginix.conf
var/etc/nginx/sites-available/myproject
etc/ngnix/conf.d/default.conf
etc/ngnix/conf.d/web.conf
I'm confused regarding each conf file role, rules, when to use one or another, are they loaded one after another, or just one, are directives overwriting ?
The nginx configuration file is called nginx.conf and on most systems is located at etc/nginx/nginx.conf.
nginx.conf may optionally contain include statements to read parts of the configuration from other files. See this document for more. Read your nginx.conf file to identify which files and directories are sourced and in which context and which order.
Some distributions are delivered with an nginx.conf file that sources additional files from directories such as /conf.d/ and /sites-enabled/.
There is also a convention on some distributions to symlink files between /sites-available/ and /sites-enabled/.
The nginx -T command (uppercase T) is useful to list the entire configuration across all the included files.
How do I get the include directory for ngnix conf files to ignore *.bak?
I back up my configuration files, for example default -> default.bak. I want to include /etc/nginx/sites-enabled/default but not /etc/nginx/sites-enabled/default.bak in nginx.conf. How do I do this?
I would recommend changing default to default.conf or another extension if possible , then include can properly differentiate from config and bak.
http {
include /etc/nginx/sites-enabled/*.conf;
}
I have hosted my website on Amazon Elastic BeanStalk. It uses nginx as proxy server and has gzip compression enabled. But when I run PageInsights on the site, it reports that many of my static content files need to be gzipped. Why is PageSpeed Insights not recognizing the compression? Is there something extra that needs to be done?
I think I actully found the answer
By enabling gzip compression on nginx, you enable it only for text/html (that is nginx default http://nginx.org/en/docs/http/ngx_http_gzip_module.html#gzip_types)
In order to enable it for other types, you have to do it explicitly. In case of beanstalk, create the following file in your project
.ebextensions/gzip.config
and put the code there (make sure you keep the indentation, it is important):
files:
/etc/nginx/conf.d/gzip.conf:
content: |
gzip_types application/json;
As you can see, in my case I needed to gzip json files, you are probably having problems with Pagespeed complaining about css and js files, right? As the link above suggests you can use a * wildcard to compress everything, but if not, just list the mime types you need in the config, deploy it, and check PageSpeed Insights again.
Dmitry's answer only works in the case where there is no gzip_types entry in the default config that amazon sets for you. This is now the case and so you will need to write an .ebextensions conf file to overwrite the entire config with a custom one. To do this you need to:
Download the default config from one of your instances via SSH. It will be in the folder /etc/nginx/conf.d and be called 00_elastic_beanstalk_proxy.conf
Create a new file in your .ebextensions folder called proxy.conf that follows this template:
files:
"/etc/nginx/conf.d/proxy.conf":
mode: "000644"
owner: root
group: root
content: |
# Paste the contents of the config you downloaded here
# at this indentation level
container_commands:
00_remove:
command: "rm -f /tmp/deployment/config/#etc#nginx#conf.d#00_elastic_beanstalk_proxy.conf /etc/nginx/conf.d/00_elastic_beanstalk_proxy.conf"
Change the config to have the gzip_types you want.
Deploy your application
For reference this is what my working proxy.conf file looks like: https://pastebin.com/raw/KGvdsZc4
Word of Caution:
I have been assured it is a common use case to overwrite the entire config this way and while it makes changing the config easier in the future this will break some functionality of the AWS EB Web Tools. Particularly anything that effects the nginx config (static file paths, gzip compression, etc) will not work. In order to make changes you will just have to change the config directly in proxy.conf
techwes solution was very helpful and worked great (in my case, allowed me to add application/javascript to the gzip_types), with one modification: the file in your .ebextensions folder must be named with a .config extension, so it should be proxy.config. (I tried to add a comment to techwes' post but don't have enough rep!)
It should also be noted that if you turn off gzip in your EB environment using the AWS Console (Environment > Configuration > Software Configuration), it will remove the gzip lines from the 00_elastic_beanstalk_proxy.conf file, so you can use a .config file to add another .conf file without having to replace the entire 00_elastic_beanstalk_proxy.conf file.
When i tried to configure the webserver through the path cd /opt/nginx/conf/ an error appears /opt/nginx/conf.Hope some one will have the answer here.Thank you
probably your config is under /etc/nginx, to check which directories nginx is using you can use the command whereis nginx