Configuring Nginx reverse proxy for the first time to solve cross-domain problems.
ringa_lee
ringa_lee 2017-05-16 17:24:16
0
1
478

I want to use the reverse proxy function of nginx to solve the problem of cross-domain requests

PM25 has an open interface. Just use GET to request the corresponding URL to return the corresponding JSON data

I want to use my own nginx as a proxy to access specific fields under the main domain name to obtain the JSON data of PM25

nginx.conf

location /get_aqi_details_hangzhou 
            {
             proxy_pass http://www.pm25.in/api/querys/pm2_5.json?city=hangzhou&token=5j1znBVAsnSf5xQyNQyq; 
             proxy_set_header Host $host;    
            }

I want to get this data by accessing the main domain name/get_aqi_details_hangzhou, and restart nginx after each configuration

In actual operation, it never succeeds and reports 404

What is the cause?

ringa_lee
ringa_lee

ringa_lee

reply all(1)
PHPzhong

I have never seen proxy_pass used like this. proxy_pass means that nginx acts as a proxy and passes the request to the specified host. So you need to rewrite the request path to what it needs to be.

        location /get_aqi_details_hangzhou {
                rewrite .* /api/querys/pm2_5.json?city=hangzhou&token=5j1znBVAsnSf5xQyNQyq break; 
                proxy_pass http://www.pm25.in; 
        }
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template