Nginx
Concepts
- Proxy (Server): An intermediary for requests from clients.
- Reverse Proxy (Server): An intermediary for responses from servers.
Systemd
Systemd is a series of commands, it replaces initd and has pid=1.
systemctl reboot
systemctl poweroff # halt, suspend, hibernate
# Unit
systemctl list-units
systemctl status
systemctl status bluetooth.service
systemctl is-active application.service
systemctl is-failed application.service
systemctl is-enabled application.service
systemctl start apache.service
systemctl stop apache.service
systemctl restart apache.service
systemctl kill apache.service
systemctl reload apache.service
systemctl enable apache.service # sudo ln -s '/usr/lib/systemd/system/apache.service' '/etc/systemd/system/multi-user.target.wants/apache.service'
systemctl disable apache.service # remove link
systemctl cat apache.service # cat config filebash
CLI
systemctl start nginx.service
nginx -s reload
nginx -s stop
nginx -s quit
nginx -t # test, check config files.
bash
Logs
default:
/var/log/nginx/access.log
/var/log/nginx/error.log
Configs
Location: /etc/nginx/nginx.conf
-
Static Website
# basic http { server { listen 80; location / { root /data; # root directory index index.html; # http://mysite.com/index.html --> /data/index.html } } } # root vs alias http { server { listen 80; location /static/ { # location is appended to root root /data; # http://mysite.com/static/ --> /data/static/ } } server { listen 80; location /static/ { # location is dropped, and instead use the alias (the trailing `/` is nececssary here!) alias /data/; # http://mysite.com/static --> /data/ } } } # auto serve static files http { server { listen 80; # http://mysite.com/files --> /data/files location /files { root /data; # serve /data/files autoindex on; # automatically show the directory tree (if off, will show 403 Forbidden, but can still access the file using fullname.) } } } # more detailed example of the trailing `/` # access: http://localhost/app/path/file # server: /root/app/path/file location /app/ { root /root/; # --> /root/app/path/file } location /app/ { root /root; # --> /root/app/path/file } location /app { root /root/; # --> /root/app/path/file } location /app { root /root; # --> /root/app/path/file } location /app { alias /root/; # --> /root/path/file } location /app { alias /root; # --> (wrong) TODO }bash -
Dynamic Website (need backend)
http { server { listen 127.0.0.1:8080; # / will redirect to uwsgi port location / { include uwsgi_params; uwsgi_pass 127.0.0.1:8000; } } }bash -
redirect
http { server { ... # make a shortname for your github repo: # kiui.moe/g<else> --> github.com/ashawkey<else> location { rewrite$1; } } }nginx -
Reverse Proxy
Proxy to local port:
http { server { # access at domain.com/app/<params> location { proxy_pass; # the trailing `/` means relative --> --> localhost:5000/<params> # proxy_pass http://127.0.0.1:5000; # w/o the `/`, it means --> localhost:5000/app/<params> # to correctly pass everything: proxy_set_header$remote_addr; proxy_set_header$remote_addr; proxy_set_header$scheme; proxy_set_header$http_host; proxy_set_header$host; } } } # more detailed example of the trailing `/` # access http://localhost/app/api/abc location { proxy_pass; # --> http://localhost:1234/api/abc (relative) } location { proxy_pass; # --> http://localhost:1234//api/abc (relative, better not use) } location { proxy_pass; # --> http://localhost:1234/app/api/abc (absolute) } location { proxy_pass; # --> http://localhost:1234/app/api/abc (absolute) }nginx -
HTTPS
http { server { listen443; # https usually use 443 server_name; # 购买的证书位置 ssl_certificate; ssl_certificate_key; ssl_session_timeout5m; ssl_ciphers; ssl_protocols; #使用该协议进行配置。 ssl_prefer_server_cipherson; location { proxy_pass; } } } # http auto redirect to https server { listen80; server_name; #将localhost修改为您证书绑定的域名,例如:www.example.com。 rewrite$host$1; #将所有http请求通过rewrite重定向到https。 location { index; } }nginx -
Load Balance
http { upstream { server; server; server; } server { ... } }nginx