How can I optimize my nginx configuration

I have nginx .conf file as below; I'm wondering if it can be write simpler:

default.conf

upstream docsapp {
    server app:8000;
}

server {

    listen 80;

    location / {
        alias /usr/share/nginx/html/;
    }

    location /admin/ {
        proxy_pass http://docsapp/admin/;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_redirect off;
    }

    location /api/ {
        proxy_pass http://docsapp/api/;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_redirect off;
    }

    location /api-token-auth/ {
        proxy_pass http://docsapp/api-token-auth/;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_redirect off;
    }

    location /docs/ {
        proxy_pass http://docsapp/docs/;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_redirect off;
    }

    location /media/ {
        add_header Access-Control-Allow-Origin *;
        alias /usr/local/src/app/media/;
        internal;
    }

    location /static/ {
        alias /usr/local/src/app/static/;
    }

    client_max_body_size 8M;

}

Location \ is Vue.js app, remaining is Django REST Framework. I've searched a web for a while but none of found solutions worke for me.

Another problem occurred during attempt to split this file into two:

vue.conf

server {

    listen 80;

    location / {
        alias /usr/share/nginx/html/;
    }

}

and

drf.conf

upstream docsapp {
    server app:8000;
}

server {

    listen 80;

    location /admin/ {
        proxy_pass http://docsapp/admin/;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_redirect off;
    }

    location /api/ {
        proxy_pass http://docsapp/api/;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_redirect off;
    }

    location /api-token-auth/ {
        proxy_pass http://docsapp/api-token-auth/;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_redirect off;
    }

    location /docs/ {
        proxy_pass http://docsapp/docs/;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_redirect off;
    }

    location /media/ {
        add_header Access-Control-Allow-Origin *;
        alias /usr/local/src/app/media/;
        internal;
    }

    location /static/ {
        alias /usr/local/src/app/static/;
    }

    client_max_body_size 8M;

}

It also didn't work. Any help would be appreciated.

1 answer

  • answered 2021-07-31 10:59 Ivan Shatsky

    I think since you do not change request URI when you proxy requests to the docsapp upstream, you can replace four location blocks where proxy_pass directive used with the following one:

    location ~ ^/(?:admin|api|api-token-auth|docs)/ {
        proxy_pass http://docsapp;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_redirect off;
    }
    

    You can't split your server block in two, only one of them will work acting as default server (see the documentation).

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum