A
A
Artyom Zubkov2013-12-26 11:35:03
Nginx
Artyom Zubkov, 2013-12-26 11:35:03

Proxying cross domain requests through nginx unified config?

There is a config for nginx.
The task is to proxy cross-domain ajax requests through your server.

server {

    listen   80;
    server_name  domain.com;

    access_log  /var/log/nginx/domain.access.log main;
    error_log  /var/log/nginx/domain.error.log info;

    root /var/www;

    location ~* ^.*\/proxy\/(?<pschema>.*)?\/(?<phost>.*)?\/.*$ {
        set $adr $pschema://$phost;

        rewrite ^.*\/proxy\/.*?\/.*?(\/.*)$ $1 break;

        proxy_pass http://domain1.com;
#       если вот так то не работает
#       proxy_pass $adr;

        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $phost;
        proxy_set_header X-NginX-Proxy true;
        proxy_redirect off;
        proxy_connect_timeout 1;
        proxy_intercept_errors on;
        expires 30;
        break;
    }

    location / {
        index index.html index.htm;
    }

    location ~* \.(jpg|ico|gif|png|css|js|svg)$ {
        access_log off;
        expires 30d;
    }
}

it seems to work if you explicitly indicate which host to do proxy_pass, and when a variable is passed to proxy_pass, it does not want to work.
proxy_pass http://domain1.com;
# если вот так то не работает
# proxy_pass $adr;

How to solve the problem so that for each new necessary domain you do not write your own location?
UPD: Config that solves the problem
server {

    listen   80;
    server_name  domain.com;

    access_log  /var/log/nginx/domain.access.log main;
    error_log  /var/log/nginx/domain.error.log info;

    root /var/www;

    location ~* ^.*\/proxy\/(?<pschema>.*)?\/(?<phost>.*)?\/.*$ {
        resolver $dns_from_your_interfaces;
        #если есть свой dns на 127.0.0.1 то можно прописать его

        set $adr $pschema://$phost;

        rewrite ^.*\/proxy\/.*?\/.*?(\/.*)$ $1 break;

        proxy_pass $adr;

        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $phost;
        proxy_set_header X-NginX-Proxy true;
        proxy_redirect off;
        proxy_connect_timeout 1;
        proxy_intercept_errors on;
        expires 30;
        break;
    }

    location / {
        index index.html index.htm;
    }

    location ~* \.(jpg|ico|gif|png|css|js|svg)$ {
        access_log off;
        expires 30d;
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
maxaon, 2013-12-26
@artzub

Try to specify the scheme
And see if you made the correct regular expressions.
And what kind of request do you have?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question