Configure a Single Nginx Domain for Both API Calls and Static Assets

This guide explains how to set up Nginx so that one HTTPS domain can handle both backend API requests and serve static resources, simplifying certificate management and improving response speed through caching.

Coder Trainee
Coder Trainee
Coder Trainee
Configure a Single Nginx Domain for Both API Calls and Static Assets

When deploying a mini‑program, the author needed an HTTPS‑enabled domain for API calls and separate domains for static assets, which required managing multiple SSL certificates. The article seeks a solution that allows a single domain to serve both purposes.

Nginx can act as a static‑resource server; when only static files are needed, it can replace a full application server.

Beyond static serving, Nginx also performs interface mapping, load balancing, and reverse‑proxy functions, making it suitable for the desired unified domain setup.

Static resources are files that never change and do not require backend processing, while dynamic resources such as JSP or templates need data returned from backend services before rendering.

To combine static resources with API routing, the author provides a concrete Nginx configuration that proxies API requests to a Tomcat instance and serves static files from a local directory, thereby enabling caching and faster response.

server {
    listen 443 ssl;
    server_name www.aaaa.cn;
    ssl_certificate 8659238_www.aaaa.cn.pem;
    ssl_certificate_key 8659238_www.aaaa.cn.key;
    ssl_session_timeout 5m;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DH3;
    ssl_prefer_server_ciphers on;

    location / {
        proxy_pass http://localhost:8080;
    }

    location ~* \.(jpg|jpeg|gif|png|swf|rar|zip|css|js|map|svg|woff|ttf|txt)$ {
        root /tools/huanrao/uploadfile;
        index index.html;
        add_header Access-Control-Allow-Origin *;
    }

    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root html;
    }
}

The configuration first defines the API proxy under location / and then maps all static file extensions to a local directory, adding a CORS header to allow cross‑origin access.

With this setup, a single domain can serve both dynamic API endpoints and cached static assets, simplifying SSL management and improving performance.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Coder Trainee
Written by

Coder Trainee

Experienced in Java and Python, we share and learn together. For submissions or collaborations, DM us.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.