Ashoka Tree

Inspiring to share!

Server: NGINX configuration for a single domain for two different ports and map url to a port.

We can easily configure Apache or NGINX server with specific port(s) to a domain or url.
In this section i would like to share how to configure NGINX web server with two different ports for a domain and then map one url to a specific port.
This way we are also forwarding the a http request to another port.
To clarify myself in easy way let me to define the task like below.

Say, port 8080 will server all the requests to the domain where API (could be REST) related requests will be served via port 8888.
So it becomes like this –
http://localsite.dev:8080 should be the normal site and
http://localsite.dev:8888/myapi/myapi_key should serve the API related requests.

To implement the task above i had configured my Ubuntu 14.04 with PHP 5.9.x, NGINX, php5fpm, ….

server {
        listen 8080;
        root /usr/share/nginx/html/localsite.dev/index.php;
        index index.php index.html index.htm;
        server_name localsite.dev;
        location ^~ /myapi {
             deny all;
        }
        location / {
            try_files $uri $uri/ /index.php;
       }
       location ~ \.php$ {
           try_files $uri =404;
           fastcgi_pass unix:/var/run/php5fpm.
           sock;
           fastcgi_index index.php;
           include fastcgi_params;
     }
}
server {
        listen 8888;
        root /usr/share/nginx/html/localsite.dev/index.php;
        index index.php index.html index.htm;
        server_name localsite.dev;
        location /myapi {
             allow all;
        }
        location / {
             deny all;
       }
      location ~ \.php$ {
           try_files $uri =404;
           fastcgi_pass unix:/var/run/php5fpm.
           sock;
           fastcgi_index index.php;
           include fastcgi_params;
     }
}

Powered by WordPress