Enable CORS with Credentials in Nginx

You must have noticed that when enable cors with “*”, it doesn’t allow credential to pass. Solution to this is pretty simply, you just need to list all of your domains in configuration. My approach is to have a separate file for each domain.

Directory Structure:

./conf/site-enabled/<site-name>
./conf/cors/<site-name>

Configuration

Assuming that site-name is webapplicationconsultant.com and I want to enable credentials for varunbatra.com along it itself – This is how it goes:

#./conf/cors/webapplicationconsultant.com


set $cors '';
if ($http_origin ~ '^https?://(varunbatra\.com|webapplicationconsultant\.com)') {
        set $cors 'true';
}

if ($cors = 'true') {
        add_header 'Access-Control-Allow-Origin' "$http_origin" always;
        add_header 'Access-Control-Allow-Credentials' 'true' always;
        add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS' always;
        add_header 'Access-Control-Allow-Headers' 'Accept,Authorization,Cache-Control,Content-Type,DNT,If-Modified-Since,Keep-Alive,Origin,User-Agent,X-Requested-With' always;
        # required to be able to read Authorization header in frontend
        #add_header 'Access-Control-Expose-Headers' 'Authorization' always;
}

if ($request_method = 'OPTIONS') {
        # Tell client that this pre-flight info is valid for 20 days
        add_header 'Access-Control-Max-Age' 1728000;
        add_header 'Content-Type' 'text/plain charset=UTF-8';
        add_header 'Content-Length' 0;
        return 204;
}
#./conf/site-enabled/webapplicationconsultant.com

server {
	listen 443 ssl;
	server_name webapplicationconsultant.com;
....	
        location / {
                include "./../cors/webapplicationconsultant.com";
                try_files $uri @app;
	}
	location @app {
...
	}
}

Tell your network!

Important Visuals Hacks in VIM

1. Numbering

VIM is not improved without custom configuration. It varies from a developer to developer but everyone will agree on having line-numbers.

set number

But do you know that you can move between lines by doing following:

: +2 ---> Two lines Below
: -2 ---> Two lines Above

Because of this and because of the fact that Y/D J/K all goes with numbers, you must be interested in showing numbers relative to the cursor.

set relativenumber
VIM Relative numbers!

2. Highlight Current Line and Column

If you are working in tabs/windows, it is a bit frustrating to mover your cursor every time just to find where are you typing! Following two lines will make your life a bit easier.

set cursorline        "highlight current line                                                                                                                            
set cursorcolumn      "highlight current column

3. Highlight Searches

Can you spot the difference between two searches? – Of course “set” is highlighted in the second screen. Here is how I did it:

set hlsearch
set incsearch

Tell your network!

Docker Services For Development Infrastructure

Most of the web developers need at least three services running in their machine namely redis, mongo and mysql.

I have composed a simply docker-compose.yml file to run MySQL, MongoDB and Redis with Docker that too with a standard ports and authentication.

version: '3.2'
services:
    redis:
        image: redis:5.0.3
        restart: always
        network_mode: host
        ports:
            - 6379:6379
        command: redis-server --requirepass a_password
    mysql:
        image: mysql:5.7
        restart: always
        network_mode: host
        ports:
            - 3306:3306
        volumes:
            - type: bind
              source: ./mysql
              target: /var/lib/mysql
        environment:
            - MYSQL_ROOT_PASSWORD=a_password
        command: mysqld --sql_mode="STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"
    mongodb:
        image: mongo:4.1.7-xenial
        restart: always
        environment:
          - MONGODB_USER="root"
          - MONGODB_DATABASE="some_db"
          - MONGODB_PASS="a_password"
          - MONGO_DATA_DIR=/data/db
          - MONGO_LOG_DIR=/dev/null
        volumes:
          - ./data/db:/data/db
        ports:
            - 27017:27017

Run

To run it, use following command:

docker-compose up -d

Tell your network!