Get the App
SLTechnology News&Howtos  ›  Servers  › 

Nginx implements static resources

Shulou Source: shulou.com Published: 2022-06-03 09:43:17 10月26日 Update

Preface

As a high-performance server, nginx has many uses. In addition to acting as a proxy and load balancer for back-end servers, another use is to cache static resources. For example, in front-end separate projects, in order to speed up the response speed of front-end pages, we can put the front-end related resources, such as html,js,css or images, into the directory specified by nginx. When accessing, you only need to use IP plus path to achieve efficient and fast access.

1. Basic environment

1. Machine planning

Hostnameiproleweb0110.0.0.7flask process (port 5000) lb0110.0.0.5 provides Nginx static resource access and reverse proxy

two。 I wrote a small flask project based on flask_restful myself, with a directory structure of

Flask-demo: https://pan.baidu.com/s/1PMx-ULDbQ5FEhlfZchWH9Q

[root@web01 ~] # tree flask-demoflask-demo ├── app │ ├── api # API file │ │ ├── base.py │ ├── home.py │ └── web_html.py │ ├── _ _ init__.py │ ├── libs # tool function │ │ ├── commons.py const .py │ └── static # static resources │ ├── html │ │ ├── home.html │ │ └── index.html │ ├── img │ │ └── │ └── js │ └── jquery-3.4.1.min.js flask_manage.sh # flask launcher This ├── manage.py └── settings.py # flask configuration file

Accessible Url:

127.0.0.1:5000/index.html127.0.0.1:5000/home.html

Install the python version and modules

Python3.6pop3// module (version does not affect much) flaskflask_sqlalchemyflask_restfulflask_sessionflask_wtfflask_scriptflask_migrate

2. Deploy Flask applications

First, we need to deploy the Flask application in web01, then delete the / static directory under the flask-demo directory, and finally start the flask process with the startup script that comes with flask-demo.

1. Move flask-demo to the / data directory

$mkdir / data$ mv flask-demo / data/

two。 Delete or move the / static/ static directory

Mv flask-demo/app/static/ / tmp/

3. Start the flask process (be sure to advance to the flask-demo directory)

$cd / data/flask-demo$ sh flask_manage.sh start

4. When we visit the two url of the current machine port 5000, we will find that these two static resources do not exist.

3. Deploy Nginx static resource agent

1. Install nginx

$yum install nginx

two。 Upload the flask-demo package to lb01 and extract it, then move the / statis static resources to the / data directory

$mkdir / data$ tar xf flask-demo.tar.gz$ cp-r flask-demo/app/static/ / data/// View static resource file / data/ └── static ├── html │ │ home.html │ └── index.html ├── img │ └── t1.jpg └── js └── jquery-3.4.1.min.js

3.nginx profile

User www;worker_processes 1 errorists log / var/log/nginx/error.log warn;pid / var/run/nginx.pid;events {worker_connections 1024;} http {include / etc/nginx/mime.types; 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 / var/log/nginx/access.log main; sendfile on; keepalive_timeout 65; upstream blog_server_pools {ip_hash Server 10.0.0.7 weight=1; 5000 weight=1;} server {listen 80; proxy_set_header X-Forwarded-For $remote_addr; proxy_set_header host $host; location / api {proxy_pass http://blog_server_pools;} location / {alias / data/static/html/ } location / static {alias'/ data/static';}

Start nginx

$nginx

4. Browsers access lb01 proxy

Tags: Resource static directory file agent server process service mobile two front end machine module version usage port script project application configuration Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology macOS Huawei vpn Xiaomi