1

How to set reverse proxy for special path in nginx?

In my Nignx's default.conf:

server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        # root   /usr/share/nginx/html;
        root /var/www/html/website;
        index  index.html index.htm;

        try_files $uri $uri/ /index.html;

        proxy_pass http://107.120.30.76:8001;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }

I only know how to config the / root directory, how can I config the special path?

such as I request the http://107.120.30.76/api/* I want to transfer to http://107.120.30.76:8000/api/*. How to config this?

1 Answer 1

2

You can add the bellow configuration into the server config:

location /api/ {
        # root   /usr/share/nginx/html;
        root /var/www/html/website;
        index  index.html index.htm;

        try_files $uri $uri/ /index.html;

        proxy_pass http://107.120.30.76:8001/api;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.