Thursday | 21 NOV 2024
[ previous ]
[ next ]

nginx Cheatsheet

Title:
Date: 2022-07-31
Tags:  cheatsheet

Setting up a static site in nginx.

Step 1: Create the config file

/etc/nginx/sites-available/new-site.conf

server {
    listen 443;
    server_name new-site.dev;
    root /path/to/folder/new-site;
}

Step 2: Symlink the config

# ln -s /etc/nginx/sites-available/new-site.conf /etc/nginx/sites-enabled/

Step 3: Trigger certbot to get SSL certificates

# certbot --nginx

This will add some extra configuration to the nginx file.

Step 4: Restart nginx

# service nginx restart

A more complex nginx file with gzip and proxy passing.

The file to edit:

vim /etc/nginx/sites-available/project-name.conf

The content:

server {
    listen 7105;
    server_name _;
    client_max_body_size 8M;

    location / {
        add_header 'Access-Control-Allow-Origin' '*';
        proxy_pass http://localhost:6105;
        proxy_redirect off;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }

    location /public {
        alias /home/nivethan/bp/projectName/public/;
    }

    gzip  on;
    gzip_http_version 1.0;
    gzip_comp_level 2;
    gzip_min_length 1100;
    gzip_buffers     4 8k;
    gzip_proxied any;
    gzip_types
        text/css
        text/javascript
        text/xml
        text/plain
        text/x-component
        application/javascript
        application/json
        application/xml
        application/rss+xml
        font/truetype
        font/opentype
        application/vnd.ms-fontobject
        image/svg+xml;

    gzip_static on;
    gzip_proxied        expired no-cache no-store private auth;
    gzip_disable        "MSIE [1-6]\.";
    gzip_vary           on;

}

To symlink the nginx conf:

ln -s /etc/nginx/sites-available/project-name.conf /etc/nginx/sites-enabled/