D
D
Drno2022-01-22 21:24:41
Nginx
Drno, 2022-01-22 21:24:41

How to change the config to add a proxy?

There is a simple file server on NGINX, it just gives files that are in the directory
. You need to add proxying to a specific site.
It would be desirable that when opening the site mysite.ru there were 2 links - like files and redirect.
When opening mysite.ru/files - a file server was opened
When mysite.ru/redirect - let's open google.com
Separately, it's clear, everything works. I do not understand how to push it into 1 config file.
Well, how html needs to be corrected, but it’s already easier, I’ll google it.

Actually the contents of the config, this is a test

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

    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # download
    autoindex on;               # enable directory listing output
    autoindex_exact_size off;   # output file sizes rounded to kilobytes, megabytes, and gigabytes

    #location / {
    #    root upload;
    #}
}
#Вот тут дальше как правильно сделать... 
 server {
    listen          80;
    server_name     redirect;
    location / {
        proxy_pass  https://www.google.com;
    }
}
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Karabanov, 2022-01-23
@Drno

You are confusing the terms redirect and proxy. If you need a redirect, then:

location /files {
        root /path/to/files/directory;
    }

    location /google {
        return 301 https://www.google.com;
    }

But, judging by the description, you need proxying:
location /files {
        root /path/to/files/directory;
    }

    location ~* /google(?<my_uri>.*) {
      proxy_set_header        Host www.google.com;
      proxy_set_header        X-Forwarded-Proto https;

      proxy_pass https://www.google.com/$my_uri;
      proxy_read_timeout  60;
      proxy_ssl_name www.google.com;
      proxy_ssl_server_name on;
      proxy_ssl_session_reuse off;

      proxy_redirect off;
    }

In this scenario, the www.google.com index page will open, but without pictures and other statics, since the paths relative to the site root are entered in the HTML code. If you do proxying in location / { ... }, then it will work as expected.
PS
When doing this kind of thing, check if it works with curl, because the browser aggressively caches responses. In particular, if the target server responds with a redirect, then the redirect will be cached, and even if the config ends up working, you'll be left wondering why the result isn't as expected.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question