Rewrite .htaccess to Nginx configuration format to proxy different URLs to different ports

267 Views Asked by At

How to use this following htaccess rules in nginx?

So if a user visits http://lovelythings.buzz/api or img it should use local port 8080.

But when tries to visit other routes http://lovelythings.buzz it is to be proxied on port 3000 or 3001

This I have already implemented on .htaccess for Apache2 server.

but on other server I have Nginx installed and Nginx does not support .htaccess.

Following is the .htaccess, I am trying to run on Nginx.

DirectoryIndex
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_HOST} dev.farrwest.com
RewriteRule  ^api(.*)$  http://localhost:8080/api$1 [P,L]
RewriteRule  ^img/(.*)$  http://localhost:8080/img/$1 [P,L]
RewriteRule ^(.*)?$ http://localhost:3001/$1 [P,L]
1

There are 1 best solutions below

0
On BEST ANSWER

After some further search.

Following configurations are working ok for me.

server {
    listen 80;
    listen [::]:80;

    server_name lovelythings.buzz www.lovelythings.buzz;

    #root /var/www/html;
    index index.html index.htm index.nginx-debian.html; 

    location / {
        proxy_pass http://localhost:3000/;
    }

    location /assets/ {
        proxy_pass http://localhost:3000/assets/;
    }

    location /api/ {
        proxy_pass http://localhost:8080/api/;
    }

    location /graphql {
        proxy_pass http://localhost:8080/graphql;
    }
  
    location /img/ {
        proxy_pass http://localhost:8080/img/;
    }
}