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 realize dynamic routing of picture server

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

Share

Shulou(Shulou.com)06/02 Report--

In this issue, the editor will bring you about how to use Nginx to achieve dynamic routing of picture servers. The article is rich in content and analyzes and describes for you from a professional point of view. I hope you can get something after reading this article.

Realization idea

To facilitate the demonstration, 192.168.1.100 is used instead of www.xxx.com

Server storage address access address server A/usr/local/upload/192.168.1.100:8080/upload server B/usr/local/upload/192.168.1.101:8080/upload

Still visit the original external address, http://192.168.1.100:8080/upload/demo.jpg

When the original address accesses 404, the attachment corresponding to server B is accessed automatically, and the URL address remains the same.

Step 1. Start the picture service of server A (192.168.1.100)

Server A needs to install Nginx. The installation steps are brief.

Server {listen 8080; server_name localhost; # for example, visiting http://192.168.1.100:8080/upload/demo.jpg location ^ ~ / upload/ {# is equivalent to visiting / usr/local/upload/demo.jpg root / usr/local;}.}

Start nginx, access http://192.168.1.100:8080/upload/demo.jpg, and now correctly access / usr/local/upload/demo.jpg of server A.

2. Start the image service of server B (192.168.1.101)

Server B also installs Nginx, with brief installation steps

# nginx configuration of server B server {listen 8080; server_name localhost; # for example, accessing http://192.168.1.101:8080/upload/demo.jpg location ^ ~ / upload/ {# is equivalent to visiting / usr/local/upload/demo.jpg root / usr/local;}.}

Start nginx, access http://192.168.1.101:8080/upload/demo.jpg, and now correctly access server B's / usr/local/upload/demo.jpg

3. Modify the nginx configuration of server A

The nginx configuration in server An is as follows

Server {listen 8080; server_name localhost; # for example, visiting http://192.168.1.100:8080/upload/demo.jpg location ^ ~ / upload/ {# is equivalent to visiting / usr/local/upload/demo.jpg root / usr/local # if you cannot access it, try to access the new path. $uri is equivalent to / upload/demo.jpg, that is, uri is also passed to @ new_uploads try_files $uri @ upload_B;} location @ upload_B {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; # address of proxy server B proxy_pass http://192.168.1.101:8080;}}

Restart the nginx of server A, delete the corresponding picture of server A, and visit http://192.168.1.100:8080/upload/demo.jpg, you will find that what you get is the picture of server B, that is, you can successfully achieve the dynamic "switching" of the picture and achieve the business goal.

Summary

Internal redirection is implemented using nginx's try_files $uri @ upload_B.

The above is the editor for you to share how to use Nginx to achieve dynamic routing of the picture server, if you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

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