S
S
Svav2015-06-11 02:44:25
Nginx
Svav, 2015-06-11 02:44:25

How to set up Nginx as a proxy?

I've been trying to set it up all day and half the night, I already have no strength (
You need xxx.com/adb to be displayed as yyy.com (with the replacement of all links from xxx.com/adb to yyy.com)
Here is a piece of code:

server {
        listen 80;
        server_name xxx.com;
        root /var/www/yyy.com/web;
location /adb/ {
            resolver 8.8.8.8;
            proxy_pass http://yyy.com;
            proxy_redirect off; 
            proxy_set_header Host $host;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Real-IP $remote_addr;
        }
    }

This config works well with ip addresses, but does not want to work with domain names.
I thought the problem was in the final server, I replaced it with other domain names for the experiment, but the result turned out to be generally strange. yandex.ru refuses to work, ya.ru seems to be working. With some sites it gives an error 502 or 404 (but then I probably started digging in the wrong direction already)
And please tell me how to block the download of a file, for example xxx.com/adb/logo.png from a remote server.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
Y
Yuri, 2015-06-13
@Svav

server {
  listen 80;
  server_name xxx.com;
  root /var/www/yyy.com/web;
  location /adb/ {
    resolver 127.0.0.1;
    rewrite ^/adb/(.*)$ /$1 break;
    proxy_pass http://yyy.com;
    proxy_redirect default; #???
    proxy_set_header Host yyy.com;
  }
}

Do not be too lazy to put named on localhost. On CentOS, for example, it is enough to run the default configuration (yum install bind && chkconfig named on && service named start). This way it will be easier and faster to resolve than to knock on googleds every time.
Further - if you do not rewrite with break, removing your location, then the request to xxx.com/adb/ will go not to yyy.com, but to yyy.com/adb/. And if there is no such url, we will get 404 or something even worse.
Further - if you are going to proxy traffic to yandex.ru, then when proxying, substitute the header "Host yandex.ru". If you leave "Host $host" as it is, then it turns out that your request will come to yandex.ru, which is looking for xxx.com on the host with yandex.ru, as a result - 404.
Further - nginx.org/ru/docs/ http/ngx_http_proxy_module.html#...- it is worth dealing with proxy_redirect. Different values ​​are possible for different nodes. Usually default is enough, but off or other parameters may well be needed.
To block xxx.com/adb/logo.png while proxying:
location ~ ^/adb/logo\.png$ {
  empty_gif;
}

In location, you can also do this:
Or instead of empty_gif, if you need to give your picture - then you can, for example, do rewrite to the desired URL and give the picture from yourself.
PS You can also make a couple of locations for static elements to process them separately and put them in the local cache - traffic is reduced and access is accelerated.

S
Sergey Usov, 2015-06-11
@rahs

server {                                                                                                                                                                                            
       listen  80;                                                                                                                                                                                        
       server_name yyy.com;                                                                                                                                                                                                                                               
                                                                                                                                                                                                                                                                                 
        location / {
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header Host $host;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_pass http://xxx.com/adb/;
        }
}

I
Igor, 2015-06-11
@merryjane

Try adding a resolver to the config:
nginx.org/en/docs/http/ngx_http_core_module.html#r...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question