3
\$\begingroup\$

I'm building a server for serving static content, and uploading images there.

Everything works, but I'm not expert in nginx and server configuration, so maybe someone sees some improvements for this.

Here is my nginx config:

server {
    listen 80;

    server_name localhost;

    location / {
        proxy_pass http://localhost:8080;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }

    location ~ \.(gif|jpg|png)$ {
        gzip_static off;
        root /var/www/assets;
    }

    location ~ \.(css|js)$ {
        gzip_static on;
        root /var/www/assets;
    }
}
  1. Users will upload and save images on this server, and I added node.js server for that with 1 view: app.get('/upload', function (req, res) {...});

    That's why I'm proxying server here:

    location / { proxy_pass http://localhost:8080; ....
    
  2. location ~ \.(gif|jpg|png)$ {`
    

    I will serve images on this server

  3. location ~ \.(css|js)$ {
    

    I will serve js & css already gzipped on this server

\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

This makes sense to me. You can add a few more directives, though.

In-memory caching of file metadata

Nginx has an in-memory cache for file metadata, which can improve performance. Here's a sample config:

open_file_cache max=10000 inactive=120s;
open_file_cache_valid 60s;
open_file_cache_min_uses 2;
open_file_cache_errors on;

Sendfile

Nginx supports the syscall sendfile, which, as I understand, can directly spit bytes on disk into the network, without intermediary reading into ram, which is obviously faster. Here's an article about sendfile and co, and a basic config:

sendfile on;
tcp_nodelay on;
tcp_nopush on;

events

Outside of the server {} block, there are a few settings you can configure.

  • epoll on linux; if you're on a different OS consult the manual
  • multi_accept is generally recommended, and causes the worker process to accept as many connections as possible.

Here's a config:

worker_processes auto;

events {
  use epoll;
  multi_accept on;
}

I'd recommend looking further into things, for example by searching "nginx static performance" or similar, there are many articles. These are what show up from the top of my head. I don't see anything wrong with your config as a base, though.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.