Skip to content

Latest commit

 

History

History
103 lines (65 loc) · 2.41 KB

nginx.md

File metadata and controls

103 lines (65 loc) · 2.41 KB

Nginx

App / web server with similar capabilities to Apache.

http://nginx.org/en/docs/

Very used by the Rails community.

Vs apache

Architecture comparison: http://www.thegeekstuff.com/2013/11/nginx-vs-apache. Nginx scales better, Apache is older and has more configuration options and libraries.

Good official beginners tutorial: http://nginx.org/en/docs/beginners_guide.html.

Configuration

Main configuration file on Ubuntu

vim /etc/nginx/nginx.conf

Serve static files

http {
    server {
        # URL /
        location / {
            root /data/www;
        }

        # URL /images/
        location /images/ {
            root /data;
        }
    }
}

proxy_pass

Serve forward requests somewhere else.

Forward everything on http to example.com:

http {
    server {
        listen 0.0.0.0:8000;
        location / {
            proxy_pass http://example.com;
        }
    }
}

Try it out:

curl -vvv localhost:8000
curl -vvv example.com

The requests are identical, except that Nginx rewrites the server HTTP header and sets it to Nginx. TODO how to prevent that?

HTTPS forward proxy

Not possible: http://superuser.com/questions/604352/nginx-as-forward-proxy-for-https

Nginx is designed to be a reverse proxy, not forward.

proxy_set_header

http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_set_header

Make the proxy modify a given request header and pass it to the proxied server.

access_log

http://nginx.org/en/docs/http/ngx_http_log_module.html#access_log

File to which nginx will log access.

Sample line generated by a request:

127.0.0.1 - - [04/Dec/2014:22:57:13 +0100] "GET / HTTP/1.1" 200 641 "-" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:34.0) Gecko/20100101 Firefox/34.0"

Breakdown:

  • 127.0.0.1: where the request was received
  • [04/Dec/2014:22:57:13 +0100]: timestamp
  • GET / HTTP/1.1 first line of request
  • 200: return status
  • 641: bytes in the body
  • "-": TODO
  • Mozilla/5.0 ...: user agent as given on the HTTP request header

Can be customized with log_format

The default log_format is:

$remote_addr - $remote_user [$time_local] "$request"
$status $body_bytes_sent "$http_referer" "$http_user_agent"

log_format

http://nginx.org/en/docs/http/ngx_http_log_module.html#log_format

TODO possible to log the entire transaction?