N
N
Nodlik2015-02-05 12:29:37
Nginx
Nodlik, 2015-02-05 12:29:37

How to redirect to Nginx without changing the url in the browser?

Good afternoon.
There was a need to do the following: there are two sites site.ru, service.ru, both on the same server). you need to make a redirect (without changing the url in the browser) like this:
site.ru/param1/value1/param2/value2... -> service.ru/site?url=param1/value1/param2/value2&site_id=1
On apache this is solved quite simple (.htaccess for site.ru):

RewriteRule  ^(.*)$ http://service.ru/site?url=$1&site_id=1 [P]

# И для статики
RewriteRule ^img/(.*\.(eot|font.css|otf|ttc|ttf|woff|js|png|jpg|jpeg|gif))$ http://service.ru/static/1/img/$1 [P]

But the problem is that service.ru and other sites are running on nginx configured on port 80.
The rewrite directive, as I understand it, only works with changing the url. I really couldn't figure it out with proxying.
Deploying apache as a frontend for nginx (no matter how scary it sounds), as I understand it, is only possible on 8080, or some other non-80 port. Since nginx is already 80 and there is no way to remove it from there ... only if you transfer all other sites to apache, which I would like to avoid ...
UPD After killing half a day, only a rather crutch solution turned out:
site.ru: 80 (Nginx)
server {
    listen 80;
    server_name site.ru  www.site.ru;

    access_log /var/log/nginx/test1.access.log;
    error_log /var/log/nginx/test1.error.log;

    location / {
       proxy_pass http://site.ru:8080;
    }
 }

site.ru:8080 (Apache)
AddDefaultCharset utf-8
Options +FollowSymLinks
RewriteEngine On

RewriteRule  ^(.*)$ http://service.ru/site?url=$1&site_id=1 [P]

# И для статики
RewriteRule ^img/(.*\.(eot|font.css|otf|ttc|ttf|woff|js|png|jpg|jpeg|gif))$ http://service.ru/static/1/img/$1 [P]

That is, the following chain: site.ru (nginx) -> site.ru:8080 (apache) -> service.ru (nginx).
It seems to work, but I'm afraid to imagine how it will affect performance :)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Stanislav Fateev, 2015-02-05
@svfat

proxy_pass http://service.ru;
proxy_set_header Host service.ru;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question