How to use nginx proxy to access static resources
1. Goal:
In order to request static resources (css, pictures, etc.) through nginx, page preview is performed through the nginx proxy.
2. Implementation effect:
Enter the nginx proxy address through the browser to access the local html file by opening the page, or you can also achieve the page preview function by accessing the proxy routing access interface.
Note : What I demonstrate is the configuration in the local windows development environment
3. Specific configuration
1. nginx configures the local static engineering agent
Find the nginx configuration file nginx.conf , configure nginx proxy
server{ listen 80; #前端门户工程 location / { alias D:/workspace/sc-multipl-static-web-project/; index index.html; }
Instructions:
D:/workspace/sc-multipl-static-web-project/ is your front-end project file path
Save the configuration file and restart nginx , enter localhost:80 in the browser to verify
2. Win10 configures the local domain name to achieve domain name access
Domain name access actually accesses the service through the corresponding IP address, and then accesses the service through the IP. If we have not activated Internet domain name, you can simulate domain name access by configuring local domain name mapping (only valid on this machine)
Open C:\Windows\System32\drivers\etc, find the hosts file, if not, add one yourself, as administrator Open the editor as identity, enter
127.0.0.1 www.chen123.com
and then open the nginx configuration file
server{ listen 80; server_name www.chen123.com; ssi on; ssi_silent_errors on; #前端门户工程 location / { alias D:/workspace/sc-multipl-static-web-project/; index index.html; } }
Save the configuration file and restart nginx, Browser input localhost:chen123 Verification
3.nginx configure page preview routing
First, you need to implement a page preview interface, the return format is String type, the content is actually the text content of html
Open the nginx configuration file again
http { include mime.types; default_type application/octet-stream; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; #cms页面预览路由 upstream cms_server_pool { server 127.0.0.1:31001 weight=10; } server{ listen 80; server_name www.xuecheng.com; ssi on; ssi_silent_errors on; #前端门户工程 location / { alias D:/workspace/sc-multipl-static-web-project/; index index.html; } #页面预览 location /cms/preview/ { proxy_pass http://cms_server_pool/cms/preview/; } } }
http://cms_server_pool/cms/preview/ is the page preview interface you want to implement, and jump to the real address by configuring routing,
upstream cms_server_pool { server 127.0.0.1:31001 weight=10; #如果有多个服务器,可以写在下面,例如 #server 127.0.0.1:31002 weight=10; }
Save the configuration file and restart nginx, enter http://cms_server_pool/cms/preview in the browser to verify
My local nginx configuration is as follows
events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; #gzip on; #cms页面预览路由 upstream cms_server_pool { server 127.0.0.1:31001 weight=10; } server{ listen 80; server_name www.xuecheng.com; ssi on; ssi_silent_errors on; #前端门户工程 location / { alias D:/workspace/sc-multipl-static-web-project/; index index.html; } #页面预览 location /cms/preview/ { proxy_pass http://cms_server_pool/cms/preview/; } } }
The above is the detailed content of How to use nginx proxy to access static resources. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

How to configure Nginx in Windows? Install Nginx and create a virtual host configuration. Modify the main configuration file and include the virtual host configuration. Start or reload Nginx. Test the configuration and view the website. Selectively enable SSL and configure SSL certificates. Selectively set the firewall to allow port 80 and 443 traffic.

You can query the Docker container name by following the steps: List all containers (docker ps). Filter the container list (using the grep command). Gets the container name (located in the "NAMES" column).

How to confirm whether Nginx is started: 1. Use the command line: systemctl status nginx (Linux/Unix), netstat -ano | findstr 80 (Windows); 2. Check whether port 80 is open; 3. Check the Nginx startup message in the system log; 4. Use third-party tools, such as Nagios, Zabbix, and Icinga.

Docker container startup steps: Pull the container image: Run "docker pull [mirror name]". Create a container: Use "docker create [options] [mirror name] [commands and parameters]". Start the container: Execute "docker start [Container name or ID]". Check container status: Verify that the container is running with "docker ps".

The methods that can query the Nginx version are: use the nginx -v command; view the version directive in the nginx.conf file; open the Nginx error page and view the page title.

Create a container in Docker: 1. Pull the image: docker pull [mirror name] 2. Create a container: docker run [Options] [mirror name] [Command] 3. Start the container: docker start [Container name]

How to configure an Nginx domain name on a cloud server: Create an A record pointing to the public IP address of the cloud server. Add virtual host blocks in the Nginx configuration file, specifying the listening port, domain name, and website root directory. Restart Nginx to apply the changes. Access the domain name test configuration. Other notes: Install the SSL certificate to enable HTTPS, ensure that the firewall allows port 80 traffic, and wait for DNS resolution to take effect.

Starting an Nginx server requires different steps according to different operating systems: Linux/Unix system: Install the Nginx package (for example, using apt-get or yum). Use systemctl to start an Nginx service (for example, sudo systemctl start nginx). Windows system: Download and install Windows binary files. Start Nginx using the nginx.exe executable (for example, nginx.exe -c conf\nginx.conf). No matter which operating system you use, you can access the server IP
