nginx
Nginx is a pretty good proxy, it's generally recommended to run it everywhere.
Install
With nix
do:
$ nix-env -i nginx-1.9.11
commands
$ nginx -s stop # quit nginx server (SIGTERM)
$ nginx -s quit # quit nginx server (SIGQUIT)
$ nginx -s reopen # reopen nginx server (SIGUSR1)
$ nginx -s reload # reload service (SIGHUP)
$ nginx -t file.conf # assert file
point nginx to different conf file
$ nginx -p "$(pwd)/" -c './conf/nginx.conf'
Configuring a static server
Nginx uses namespaces and directives for its configuration. Directives for a static server:
- listen: the port the server will listen at
- server_name: match the URL and apply the rules on it
- root: directory (static) files are stored in
- location: takes string / regex and a block.
try_files: attempt to read files in a certain order. Also looks for
.html
extentions and matches those
server {
listen 80;
server_name example.com;
root /var/www/example;
location / {
try_files $uri $uri/ /index.html;
}
}
match exact string with location
location = / { ... }
variables
- $uri: uri that was received
Example config
events {
worker_connections 1024;
}
http {
server {
listen 127.0.0.1:3000;
server_name localhost;
access_log /tmp/localhost.log;
charset utf-8;
location / {
proxy_pass http://127.0.0.1:1337/;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-Nginx-Proxy true;
proxy_redirect off;
}
}
}
example root config
daemon off;
pid /var/run/nginx.pid;
user www;
error_log stderr notice;
worker_processes auto;
events {
multi_accept on;
use epoll;
worker_connections 1024;
}
http {
# Somehow it's not inherited by vhosts (server{} context) when using with 'stderr' value.
# Therefore it's re-defined here to avoid specyfing it for each vhost.
error_log stderr notice;
include /etc/nginx/nginx.d/*.conf;
include /data/conf/nginx/nginx.d/*.conf;
include /etc/nginx/addon.d/*.conf;
include /data/conf/nginx/addon.d/*.conf;
include /etc/nginx/hosts.d/*.conf;
include /data/conf/nginx/hosts.d/*.conf;
}
start nginx in non daemon mode
# nginx.conf
daemon off;
OpenResty
OpenResty allows extending nginx with lua to turn it into a full-fledged webserver.
directives
- access_by_lua
- content_by_lua
- header_filter_by_lua
HTTP2
server {
listen 80 default_server;
## Needed when behind HAProxy with SSL termination + HTTP/2 support
listen 81 default_server http2 proxy_protocol;
listen 443 default_server ssl http2;
ssl_certificate /etc/ssl/dummy.crt;
ssl_certificate_key /etc/ssl/dummy.key;
root /data/www/default;
index index.html;
}