본문 바로가기

서버/Nginx

Nginx Beginner’s Guide

Configuration File’s Structure

  • nginx modules are controlled by directives defined in /etc/nginx/nginx.conf.
  • two types of directives
    • simple directive: name and parameters separated by spaces and ends with ;, e.g., listen 8080;
    • block directive: name and parameters separated by spaces and ends with a body of curly braces ({}) which contains additional instructions and directives, called a context, e.g., events, http, server, location.
    • events and http reside main context, location in server in http, so like below
      events
      http {
        server {
            location 
        }
      }
  • # makes command line as comments, e.g., # events

serving static contents

# /etc/nginx/nginx.conf
http {
    server {
        listen 80;
        location / {
            root /data/www
        }
        location /image/ {
            root /data
        }
    }
    server {   
        ...
    }
    server {   
        ...
    }
}
  • first, multiple server directives can be defined.
  • they are distinguished by ports to which it listen and by server_names.
  • once which server takes care of a request, location attempts to match given URI.
  • matching URI is appended to the prefix defined in root
    • e.g., a request http://localhost/images/example.png maps to a static file /data/images/example.png on the local machine.
    • e.g., a request http://localhost/some/example.html maps to a static file /data/www/some/example.html on the local machine.
    • nginx returns 404 error if no such file exists in the path.

setting up a proxy server

server {
    listen 8080;
    root /data/up1;

    location / {

    }
}
server {
    listen 80;
    location / {
        proxy_pass http://localhost:8080;
    } 
#   location /image/ {
    location ~ \.(gif|jpg|png)$ {
        root /data;
    }
}
  • in the fisrt server, note that root directive is in the context of server instead of location as above.
  • when there is no root specified within location, it falls back to it.
  • in the second server block, a request is filtered for image files with the extensions of gif, jpb, or png. All the other requests are passed to proxied server.

setting up fastCGI proxying

server {
    location / {
        fastcgi_pass localhost:9000;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param QUERY_STRING  $query_string;
    }
}
  • not much to say about for now..

REFERENCES

Beginner’s Guide (link)

'서버 > Nginx' 카테고리의 다른 글

nginx basics  (0) 2020.07.25
우분투 16.04 nginx 1.10 에서 1.16으로 업그레이드  (0) 2020.07.25