I am reading nginx beginner's tutorial, on the section Serving Static Content they have
http { server { } }
but when I add an http block I get the error
[emerg] "http" directive is not allowed here …
When I remove the http block and change the conf file to this, it works fine:
server { listen 80 default_server; listen [::]:80 default_server ipv6only=on; root /var/example.com/html; index index.html index.htm; # make site accessible from http://localhost/ server_name localhost location / { try_files $uri $uri/ /index.html; }
I suspect that I am missing something simple, but why do they use http to serve static files?
What is the Http Block? The http block includes directives for web traffic handling, which are generally known as universal . That's because they get passed on to each website configuration served by NGINX. File: /etc/nginx/nginx.conf.
Nginx server blocks are located in the /etc/nginx/sites-available directory. The default Nginx server block is /etc/nginx/sites-available/default which serves the default HTML file at /var/www/html/index.
NGINX Config: Directives, Blocks, and Contexts All NGINX configuration files are located in the /etc/nginx/ directory. The primary configuration file is /etc/nginx/nginx. conf .
Your doing fine. I guess you are editing /etc/nginx/sites-enabled/default (or the linked file at /etc/nginx/sites-available/default.
This is the standard nginx set up. It is configured with /etc/nginx/nginx.conf which contains the http {} statement. This in turn contains an "include /etc/nginx/sites-enabled/*" line to include your file above with server{ } clause in it.
Note that if you are using an editor that creates a backup file, you must modify the include statement to exclude the backup files, or you will get some "interesting" errors! My line is
include /etc/nginx/sites-enabled/*[a-zA-Z]
which will not pick up backup files ending in a tilde. YMMV.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With