Sub topic 4
Serving static files directly, sending the rest to the app
Assume you know basic Linux permissions, HTTP status codes, and how to edit an Nginx configuration. You also need a PHP or Python application that already runs locally. This chapter focuses on deciding which requests Nginx can answer itself and which requests must reach the application.
Static and dynamic content
Static content is a file that Nginx can read and return without running application code. Common examples include:
- HTML, CSS, and JavaScript files
- Images, fonts, and downloadable documents
- Versioned assets such as
app.4f21.js
Dynamic content is produced when the application handles a request. Examples include:
- A page that reads a user’s account from a database
- A login request
- An API endpoint
- A URL that does not correspond to a file on disk
Serving static files in Nginx is usually faster and simpler. Nginx handles efficient file transfer, while the application spends its resources on work that requires code.
Request routing with `location`
Nginx uses server blocks to select a site and location blocks to select how a URI is handled. A common design gives static assets their own URI prefix and forwards all other requests to the application.
server {
listen 80;
server_name example.com;
location /static/ {
alias /srv/myapp/static/;
}
location / {
proxy_pass http://unix:/run/gunicorn/app.sock;
proxy_set_header Host $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;
}
}With this configuration, a request for /static/site.css maps to /srv/myapp/static/site.css. A request for /orders/42 goes to Gunicorn through the Unix socket instead.
The alias directive maps the complete matching URI prefix to a directory. Keep the trailing slash on both /static/ and /srv/myapp/static/. A missing slash can produce an unexpected file path.
A PHP deployment often uses a different boundary. Nginx serves existing assets from a document root and sends PHP requests to PHP-FPM through FastCGI. Python applications commonly use a separate application server, such as Gunicorn, and Nginx communicates with it using HTTP over a socket or port.
Choosing a safe static directory
Place only public files under the static directory. Do not put configuration files, environment files, private uploads, database backups, or source-control metadata there.
A useful layout separates public assets from application code:
/srv/myapp/
├── app/
├── static/
│ ├── site.css
│ └── app.js
└── config/
└── production.envThe Nginx user needs read and directory-traversal permission for static/. It does not need write access. Application uploads should use a separate directory and should not automatically be executable or publicly readable.
Trace one request
Exercise
A request for /static/missing.css does not reach the application with this configuration. What should you check first?
Show answer
Check whether /srv/myapp/static/missing.css exists, whether the alias path and trailing slashes are correct, and whether Nginx can read the file and its parent directories.