Skip to content

Instantly share code, notes, and snippets.

@KunalSin9h
Last active May 20, 2024 07:15
Show Gist options
  • Save KunalSin9h/4dce3a8ae17980a16eae0b9fc3c0e238 to your computer and use it in GitHub Desktop.
Save KunalSin9h/4dce3a8ae17980a16eae0b9fc3c0e238 to your computer and use it in GitHub Desktop.
server {
    server_name your_domain www.your_domain;
    
    client_max_body_size 20M;

    location / {
        proxy_pass http://localhost:9990;
    }
}
@KunalSin9h
Copy link
Author

Set the client max body size client_max_body_size 20M;

@KunalSin9h
Copy link
Author

Add header

server {
    listen 80;
    server_name example.com;

    location / {
        add_header 'Access-Control-Allow-Origin' '*';
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
        add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';

        # Add your other server configurations here
    }
}

@KunalSin9h
Copy link
Author

Nginx More Gists.

1. Tools require to build the nginx from source

sudo apt install build-essential libpcre3 libpcre3-dev zlib1g zlib1g-dev libssl-dev

@KunalSin9h
Copy link
Author

KunalSin9h commented Feb 22, 2023

2. While building nginx from source we need to give config flages

./configure --sbin-path=/usr/bin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --with-pcre --pid-path=/var/run/nginx.pid --with-http_ssl_module

and some additional 1'st and 3'rd party modueles

... --with-http_ssl_module

To see all 1'st party modules that can be used go to https://nginx.org > Modules reference
And 3'rd party modules can be found at https://www.nginx.com/resources/wiki/modules/

@KunalSin9h
Copy link
Author

Some time nginx will behave weird then just force fully kill and restart

sudo pkill -f nginx & wait $!
sudo systemctl start nginx

@KunalSin9h
Copy link
Author

Create an mirror website (where you can host static files to download)

server {
  listen 80;
  server_name mirror.kunalsin9h.dev;

  location / {
     autoindex on;
     root /home/k9/mirrors;
  }
}

@KunalSin9h
Copy link
Author

KunalSin9h commented Aug 10, 2023

Caching

http {
    proxy_cache_path /api/cache levels=1:2 key_zone=api_cache:100m max_size=10g inactive=168h use_temp_path=off;

   server {
     location ~ /v1/image/.* {

        add_header Cache-Control public;
        add_header Pragma public;
        add_header Vary Accept-Encoding;
        add_header X-Cache-Status $upstream_cache_status;

        proxy_cache api_cache;
        proxy_cache_valid 200 168h;

        proxy_pass http://127.0.0.1:9999;
    }
  }
}

Caution

proxy_cache_valid is require for cache to work.

proxy_cache_valid 200 168h;

proxy_cache_valid 200 168h is required for nginx caching to work.

@KunalSin9h
Copy link
Author

Disable Request and Response Buffering

Tip

Helpful in Server Sent Event like Event Stream.

Response Buffering:

server {
  location / {
    proxy_buffering off;
  }
}

Request Buffering:

server {
   proxy_request_buffering off; 
   client_body_buffer_size 0;
   proxy_max_temp_file_size 0;
}

@KunalSin9h
Copy link
Author

Proxy Set Headers

proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_http_version 1.1;
proxy_pass http://unix:/run/gosvc/SERVICE.sock:/URL;
proxy_redirect default;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment