Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When do we need to use http block in nginx config file?

Tags:

http

nginx

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?

like image 623
Zeynel Avatar asked Dec 17 '13 16:12

Zeynel


People also ask

What is http block in Nginx?

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.

Where do I put Nginx server block?

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.

In which directory are Nginx server block configuration files stored?

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 .


1 Answers

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.

like image 190
Ian Avatar answered Sep 21 '22 10:09

Ian