Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use nginx to solve cross-domain problems

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces the relevant knowledge of "how to use nginx to solve cross-domain problems". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

As shown in the figure:

In order to reduce typing code and improve productivity, we certainly want to cut down the middle tier of python, but how to solve the following three problems is the key:

Backend rendering

Login status determination

Cross-domain forwarding api

I will describe it in detail in two other blogs about 1BI 2. This article mainly deals with 3, that is, cross-domain problems. There are many ways to solve cross-domain problems: reverse proxy, jsonp,cross-origin resource sharing, etc., which we implement through nginx reverse proxy today.

Create two new flask programs to experiment

Open pycharm, and select flask,name for the new project as client and server, respectively.

Write python files for client and server to run on ports 5000 and 5001, respectively:

Client.py

From flask import flaskapp = flask (_ _ name__) @ app.route ('/') def hello_world (): return 'this is client'if _ _ name__ =' _ _ main__': app.run (port=5000)

Server.py

From flask import flaskapp = flask (_ _ name__) @ app.route ('/') def hello_world (): return 'this is server' @ app.route (' / api/') def api (): return 'api'if _ _ name__ = =' _ main__': app.run (port=5001)

Run client.py

Run server.py

Install nginx (ubuntu)

Open Synaptic, search for nginx, select and install. Ubuntu is so simple, other platforms do not describe it for the time being, but can search on their own.

Configure nginx to forward requests from port 5000 (client) to port 5001 (server)

Open the default profile for nginx:

Sudo gedit / etc/nginx/sites-available/default

Add the following command at the end of the file:

# # demo listen 5017 proxy 5000 and 5001 # # server {listen 5017; server_name a.xxx.com; access_log / var/log/nginx/a.access.log; error_log / var/log/nginx/a.error.log; root html; index index.html index.htm index.php; # # send request back to flask # # location / {proxy_pass http://127.0.0.1:5000/; # proxy settings proxy_redirect off; proxy_set_header host $host Proxy_set_header x-real-ip $remote_addr; proxy_set_header x-forwarded-for $proxy_add_x_forwarded_for; proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504; proxy_max_temp_file_size 0; proxy_connect_timeout 90; proxy_send_timeout 90; proxy_read_timeout 90; proxy_buffer_size 4k; proxy_buffers 4 32k; proxy_busy_buffers_size 64k } location / proxy {rewrite ^. + proxy/? (. *) $/ $1 break; proxy_pass http://127.0.0.1:5001/; # proxy settings proxy_redirect off; proxy_set_header host $host; proxy_set_header x-real-ip $remote_addr; proxy_set_header x-forwarded-for $proxy_add_x_forwarded_for; proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504; proxy_max_temp_file_size 0 Proxy_connect_timeout 90; proxy_send_timeout 90; proxy_read_timeout 90; proxy_buffer_size 4k; proxy_buffers 4 32k; proxy_busy_buffers_size 64k;} # # end a.xxx.com # #

Run nginx:

Sudo / etc/init.d/nginx restart

These commands cause localhost:5017 to proxy localhost:5000, as shown in the figure:

Causes localhost:5017/proxy to proxy localhost:5001, as shown in the figure:

Causes localhost:5017/proxy/api/ to proxy localhost:5001/api/, as shown in the figure:

As a result, the url that originally needed to request port 5001 from port 5000 became the / proxy that requested port 5017 from port 5017. The cross-domain problem caused by the same origin strategy is solved.

This configuration file can also be used with uwsgi, or you can run the python file directly to start the service without uwsgi. This article is the latter.

This is the end of "how to use nginx to solve cross-domain problems". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report