Home  >  Q&A  >  body text

Configuring Nginx reverse proxy for the first time to solve cross-domain problems.

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_leeringa_lee2712 days ago438

reply all(1)I'll reply

  • PHPz

    PHPz2017-05-16 17:26:16

    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; 
            }
    

    reply
    0
  • Cancelreply