C
C
chidan2020-11-01 20:08:06
Backend
chidan, 2020-11-01 20:08:06

Is it possible to host an API backend and a web application on the same server?

There is a backend written in Python Flask and running through Nginx. Mobile applications and microcontrollers work with it via API. During development, we also wrote a web application that also worked via the API and was hosted on a separate server.
Now there is a need to place the backend and the web application on the same server. And I don't really understand yet how to place them together without breaking anything. Is it possible to host a web application in the form of a second Nginx server (well, or transfer the back to Apache - there were such plans), while saving work on the API both for the web application itself and for all other clients?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir Korotenko, 2020-11-01
@firedragon

In the location you write location /api/mapapi/ and proxypass to your upstream. Add custom headers as needed. Look at the article kestrel on nginx it is described in detail, but this is valid for any bundles
Config for nginx with apache backend, net core api proxying, and vue SPA

server {
        server_name test.com;
        ssl_certificate "/var/www/httpd-cert/www-root/test_com_le2.crtca";
        ssl_certificate_key "/var/www/httpd-cert/www-root/test_com_le2.key";
        ssl_ciphers EECDH:+AES256:-3DES:RSA+AES:!NULL:!RC4;
        ssl_prefer_server_ciphers on;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_dhparam /etc/ssl/certs/dhparam4096.pem;
        charset off;
        index index.php index.html;
        disable_symlinks if_not_owner from=$root_path;
        include /etc/nginx/vhosts-includes/*.conf;
        include /etc/nginx/vhosts-resources/test_com/*.conf;
        access_log /var/www/httpd-logs/test_com.access.log;
        error_log /var/www/httpd-logs/test_com.error.log notice;
        ssi on;
        set $root_path /var/www/www-root/data/www/test_com/public;
        root $root_path;
        location / {
                location ~ [^/]\.ph(p\d*|tml)$ {
                        try_files /does_not_exists @fallback;
                }
                location ~* ^.+\.(jpg|jpeg|gif|png|svg|js|css|mp3|ogg|mpe?g|avi|                                                                                                                                                                                                                                             zip|gz|bz2?|rar|swf)$ {
                        try_files $uri $uri/ @fallback;
                }
                location / {
                        try_files /does_not_exists @fallback;
                }
        }
        # тут хранится статика SPA  
        location /mks/ {
                       root /var/www/www-root/data/www/test_com;
                }
         # Прокси для api
        location /api/mapinfo/ {
            proxy_pass         http://localhost:5010/api/mapinfo;
            proxy_http_version 1.1;
            proxy_set_header   Upgrade $http_upgrade;
            proxy_set_header   Connection keep-alive;
            proxy_set_header   Host $host;
            proxy_cache_bypass $http_upgrade;
            proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header   X-Forwarded-Proto $scheme;
        }
        location @fallback {
                proxy_pass http://127.0.0.1:8080;
                proxy_redirect http://127.0.0.1:8080 /;
                proxy_set_header Host $host;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header X-Forwarded-Proto $scheme;
                proxy_set_header X-Forwarded-Port $server_port;
                access_log off;
        }
        listen 123.123.123.123:443 ssl;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question