Thursday, 22 March 2018

Reverse Proxy serve different folder NGINX

In this post i'll show you how we can configure NGINX to serve applications form the different directories. I have two application both are running at different ports, 8080 and 8090. I want to expose those APIs just like they are serving from a single source.

I want to expose those APIs just like they are serving from a single source. nginx config file /etc/nginx/site-available

server {
 
    listen              443 ssl;
    server_name         app.yourapp.com;
    ssl_certificate     /etc/nginx/ssl/app.yourapp.com.crt;
    ssl_certificate_key /etc/nginx/ssl/app.yourapp.com.key;
 
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;
    ssl_ciphers 'EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH';
 
    location /v1/ {
            proxy_pass http://localhost:8090;
            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 / {
        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;
 }
}

If you add multiple application at same host. we have configured NGINX reverse Proxy for two apps using location block, v1 is listening at 8090 and / is listening at 8080.

You can access the app : https:://app. yourapp.com and https:://app.yourapp.com/v1

If you are new to nginx server so you can check this link to use nginx.

Install Typescript in ubuntu

For now I just know of the one main way to install Typescript, but as and when I find out more ways, such as a PPA, I'll post them here.

sudo apt update && sudo apt install node-typescript -y

Finally Clause in PHP

PHP has supported the finally clause.Finally will executed after a try block no matter what is your condition. if the try block threw an exception or not.

<?php

try 
{
    print "Start executing try block BEFORE exception." . PHP_EOL;
    throw new Exception("This is my exception");
    print "Executing try block AFTER exception thrown." . PHP_EOL;
} 
catch (Exception $e) 
{
    print "Executing the catch block." . PHP_EOL;
} 
finally 
{
    print "Executing 'finally' block." . PHP_EOL;
}

print "Executing code after finally." . PHP_EOL;

Start executing try block BEFORE exception.
Executing the catch block.
Executing 'finally' block.
Executing code after finally.

so finally is executed even if exception occurred. we can understand this with the below two examples.

try {
    throw_exception();
} catch (ExceptionTypeA $e) {
    echo $e->getMessage();
}

example(); // Will not execute if throw_exception throws an ExceptionTypeB

But this will execute.

try {
    throw_exception();
} catch (ExceptionTypeA $e) {
    echo $e->getMessage();
} finally {
    some_code(); // Will be execute even if throw_exception throws an ExceptionTypeB
}

As you can see the finally block is still executed, even though something went wrong within our exception handling.

Featured post

How to create dynamic schema in mongodb

Sometime what we need in our application is to create mongodb schema for collection and some of our collection schema are same but names are...

Popular Posts