- ๐จโ๐ป Shrawan Poudel
~ nginx -s SIGNAL_LISTS
~ cat /etc/nginx/nginx.conf
use : server block
http {
# Configuration specific to
# HTTP and affecting all virtual servers
server {
# configuration of TCP virtual server 1
}
server {
# configuration of TCP virtual server 2
}
}
~ nginx -s reload
~ nginx -t
Serving static content
server {
server_name
listen 80;
root
location
}
location /static/ {
# just do anything โบ๏ธ
}
location /media/ {
# just do anything โบ๏ธ
}
~ ls /home/static/
- ok.html
# nginx server block
server {
root /home/static/;
location / {
}
}
~ curl yourdomain.com/
- Got a 404 ?
server {
root /home/static/;
location / {
autoindex on;
}
}
Got a auto generated directory listing ? Cool ๐
index to the rescue!!!
server {
root /home/static/;
location / {
index ok.html;
}
}
server {
location /static-root/ {
root /home/ubuntu/static-root/;
}
location /static-alias/ {
alias /home/ubuntu/static-alias/;
}
}
/static-root/
- appends the location
- full path : /home/ubuntu/static-root/static-root/
- Correct it:
- root /home/ubuntu/
/static-alias/
- location parts get dropped
- full path: /home/ubuntu/static-alias/
- Bonus:
Remember the trailing slash on alias
server {
root /home/ubuntu/
location /images/ {
try_files $uri /images/default.gif;
}
}
server {
root /home/ubuntu/
location / {
try_files $uri $uri/ $uri.html =404;
}
}
server {
location / {
try_files $uri $uri/ @backend;
}
location @backend {
proxy_pass http://backend.example.com;
}
}
location / {
proxy_pass http://localhost:8085;
}
To pass a request to a non-HTTP proxied server, the appropriate **_pass directive should be used:
use proxy_set_header
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Accept-Encoding "";
location / {
proxy_bind 127.0.0.2;
proxy_pass http://example.com/;
}
use of allow or deny directive
location /api {
#...
deny 192.168.1.2;
allow 192.168.1.1/24;
allow 127.0.0.1;
deny all;
}
Note the Order of allow and deny directive
Group the servers with upstream directive
upstream backend {
server backend1.example.com;
server backend2.example.com;
server 192.0.0.1;
}
server {
location / {
proxy_pass http://backend;
}
}
Load balancing Method
Nginx Error Log Severity Levels
- debug - Useful debugging information to help determine where the problem lies.
- info - Informational messages that arenโt necessary to read but may be good to know.
- notice - Something normal happened that is worth noting.
- warn - Something unexpected happened, however is not a cause for concern.
- error - Something was unsuccessful.
- crit - There are problems that need to be critically addressed.
- alert - Prompt action is required.
- emerg - The system is in an unusable state and requires immediate attention.
use directive log_format
log_format compression '$remote_addr - $remote_user [$time_local] '
'"$request" $status $body_bytes_sent '
'"$http_referer" "$http_user_agent" "$gzip_ratio"';
#Using this compression log format on access_logs
access_log /spool/logs/nginx-access.log compression;
server {
access_log /home/ubuntu/logs/nginx_access.log;
error_log /home/ubuntu/logs/nginx_error.log;
server_name api.example.com;
location /static/ {
alias /home/ubuntu/static/;
}
if ($host !~* ^(api.example.com)$ ) {
return 444;
}
location / {
.........
proxy_pass http://127.0.0.1:8085;
.........
}
listen 443 ssl;
ssl_certificate SSL_CERTIFICATE_LOCATION;
ssl_certificate_key SSL_CERTIFICATE__KEY_LOCATION;
}
server {
if ($host = api.example.com) {
return 301 https://$host$request_uri;
}
listen 80;
server_name api.example.com;
return 404;
}
Made with ๐ by Shrawan
~ with help of