Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use Nginx to realize dynamic routing of picture server

Shulou Source: shulou.com Published: 2022-06-02 10:28:48 09月22日 Update

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.

Tags: Services servers pictures addresses dynamic steps configuration routing content analysis success professional business small and medium-sized rich content that is ideas articles more goals Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux MariaDB Docker vpn NVidia