Skip to content

Instantly share code, notes, and snippets.

@mehdishahdoost
Created April 25, 2023 19:10
Show Gist options
  • Select an option

  • Save mehdishahdoost/b87356c2bcf1f1de0e273da2ee14e854 to your computer and use it in GitHub Desktop.

Select an option

Save mehdishahdoost/b87356c2bcf1f1de0e273da2ee14e854 to your computer and use it in GitHub Desktop.
Nginx

Nginx features

  • Web Server
  • Load Balancer
  • Reverse Proxy
  • Monitoring and Management

HTTP Codes

  • 100-199 : Information status codes
  • 200-299 : Success status codes
  • 300-399 : Redirection status codes
  • 400-499 : Client error status codes
  • 500-599 : Server error status codes

example:

  • 301: Move Permanently
  • 304: Not modified
  • 401: Un-Authorized
  • 403: Forbidden
  • 404: Page not found
  • 500: Internal Server Error

Install Nginx by docker

docker pull nginx

Run nginx by docker

docker run -d --name nginx-test -p 8080:80 nginx

Connect and login to running nginx

docker exec -it docker-name bash

Stop and Start nginx service

service nginx start
service nginx stop
service nginx status

nginx configs

/conf/nginx/nginx.conf

user nginx;
worker_processes auto;
  • worker_processes auto; it means nginx master creates worker processes by core cpu number.
  • worker_processes 1; this one creates 1 worker process.

Validation nginx configuration

nginx -t

default location of nginx html files

/usr/share/nginx/html/

simple nginx server


server {

server_name localhost;

location / {
  root /var/www;
  index index.html;   
}

}

we can validate config file with nginx -t and after that we can reload nginx server:

service nginx reload

How can we create reverse proxy with nginx


server {

server_name localhost;

location / {
    proxy_pass http://google.com
}

}

directive syntax: proxy_pass ;

Pass all client header by nginx proxy

proxy_set_header Host $host;
proxy_set_header X-Forwarded-By $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment