Nginx reverse proxy static files and modify path methods
Nginx reverse proxy static files and modify paths
Nginx configuration wants to/a/b/
Request proxy to local directory/abc/
file under. Can be used in Nginx configurationalias
Directive to specify a local path as the proxy target.
server { listen 8080; location / { proxy_pass http://192.168.1.100:8080; } location /a/b/ { alias /abc/; try_files $uri $uri/ /; } }
Explanation and precautions:
-
location /a/b/
: A location block is configured here to match/a/b/
The request at the beginning. -
alias /abc/;
: Use the alias directive to specify the local path/abc/
As a proxy target. When matched/a/b/
When requests are made, Nginx will map these requests to the local directory/abc/
。 -
try_files $uri $uri/ /;
: Used heretry_files
Directive to try to find the corresponding file. If the requested file does not exist, it will return/
。
Notice:
- In use
alias
The ending slash when instructed/
It is important to make sure the path is set correctly. - Need to make sure Nginx is right
/a/b/
The access permissions and paths are configured correctly, as well as the local directory/abc/
Contains the required static files or resources. - After the configuration is complete, restart or reload Nginx and try to access
/a/b/
The resource under it should be proxyed to the local directory/abc/
corresponding file in.
Nginx reverse proxy + path rewrite configuration
#user nobody; worker_processes 1; #error_log logs/; #error_log logs/ notice; #error_log logs/ info; #pid logs/; events { worker_connections 1024; } http { include ; default_type application/octet-stream; #log_format main '$remote_addr - $remote_user [$time_local] "$request" ' # '$status $body_bytes_sent "$http_referer" ' # '"$http_user_agent" "$http_x_forwarded_for"'; #access_log logs/ main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/ main; # Access method: http://127.0.0.1/1/ng1/ --> http://127.0.0.1:82 The main difference between here and method is the location of rewrite and proxy_pass. location ~ /ng1/ { proxy_pass http://127.0.0.1:81; rewrite "^/(.*)/ng1\/(.*)$" /$2 break; } # Access method: http://127.0.0.1/1/ng2/ --> http://127.0.0.1:82 location ~ /ng2/ { rewrite "^/(.*)/ng2/(.*)$" /$2 break; proxy_pass http://127.0.0.1:82; } # Access method: http://127.0.0.1/ng3/ --> http://127.0.0.1:83 location /ng3/ { rewrite ^/ng3/(.*)$ /$1 break; proxy_pass http://127.0.0.1:83; } #error_page 404 /; # redirect server error pages to the static page / # error_page 500 502 503 504 /; location = / { root html; } }
Summarize
The above is personal experience. I hope you can give you a reference and I hope you can support me more.
Related Articles
Example of implementation of k8s deployment nginx access Tomcat
This article introduces how to use Kubernetes to deploy Nginx and access Tomcat through Nginx, which has certain reference learning value for everyone's learning or work. Friends who need it, please learn with the editor below.2023-08-08Nginx sets Access-Control-Allow-Origin multi-domain cross-domain implementation
This article mainly introduces the cross-domain implementation of Access-Control-Allow-Origin multi-domain name settings. The example code is introduced in this article in detail, which has certain reference learning value for everyone's learning or work. If you need it, please follow the editor to learn together.2024-11-11How to configure mod_proxy reverse proxy in Nginx server
This article mainly introduces the method of configuring mod_proxy reverse proxy in Nginx server. The biggest feature of Nginx server is to use it as a high-performance reverse proxy. Friends who need it can refer to it.2015-07-07How to integrate Nginx into Windows services
The article introduces how to configure Nginx as a system service on Windows 11, and provides a detailed step guide, including downloading and configuring Winsw tools, as well as ways to install and delete Nginx services.2024-12-12Nginx access log and error log parameter description
This article mainly introduces the Nginx access log and error log parameter description. The example code is introduced in this article in detail, which has certain reference value for everyone's learning or work. Friends who need it can refer to it.2020-11-11Complete steps to redirect www to non-www using Nginx on CentOS 7
This article introduces how to use Nginx to configure domain name redirection on CentOS7 and set corresponding DNS records. Redirection not only helps to improve the search engine ranking of the website, but also ensures that users can get a consistent website experience whether they access through www or non-www domain names. Friends who need it can refer to it.2024-11-11Detailed explanation of commonly used nginx commands into shell scripts
This article mainly introduces the detailed explanation of the commonly used nginx commands into shell scripts. The article explains it very clearly. Interested students can study it.2021-02-02Steps to upgrade https under Nginx
This article mainly introduces the methods and steps for upgrading https under Nginx. The editor thinks it is quite good. I will share it with you now and give you a reference. Let's take a look with the editor2019-06-06Implementation steps for Nginx configuring SSL certificates
This article details how to configure SSL certificates for Nginx servers to ensure secure access through HTTPS. The steps include DNS resolution verification, downloading and installing SSL certificates, installing Nginx, configuration files, and setting up HTTPS redirection. Those who are interested can learn about it.2024-09-09A detailed summary of the matching order of Nginx configuration location
This article mainly introduces the detailed explanation of the Nginx configuration location matching sequence. Nginx is a very lightweight HTTP server. With its stability, low resource consumption, simple configuration and rich functions, Nginx has developed from an unknown Web server software more than ten years ago to a position that can match Apache. Friends who need it can refer to it.2023-08-08