Caching web pages using Nginx
I tried to use Nginx server in the past few days and checked the purpose of this server. I found that this server can be used to implement functions such as reverse proxy and load balancing. I realized the function of caching web pages by searching for information. The following is the setting of my Nginx configuration file. The path of the configuration file is: /usr/local/nginx /conf/nginx.conf
Here are two detailed introductions about nginx.conf:
Complete configuration instructions for nginx.conf
Detailed explanation of Nginx installation and configuration file nginx.conf
Nginx Location configuration summary
The following is the file content of my nginx.conf:
#用户组 用户 #user nobody; #工作进程,根据硬件调整 worker_processes 1; #错误日志 #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid文件位置 #pid logs/nginx.pid; events { #工作进程的最大连接数 worker_connections 1024; } http { #设定mime类型 include 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 logs/access.log main; #开启高效文件传输模式,sendfile指令指定nginx是否调用sendfile函数来输出文件, #对于普通应用设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为off, #以平衡磁盘与网络I/O处理速度,降低系统的负载。注意:如果图片显示不正常把这个改成off。 sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; #设置缓存路径和名称为webserver,内存缓存空间大小为200MB,30天没有被访问的内容自动清除,硬盘缓存空间大小为30GB。 proxy_cache_path /usr/local/nginx/webserver levels=1:2 keys_z inactive=3d max_size=30g; server { #指定虚拟主机的服务端口 listen 1025; #制定虚拟主机的IP地址 server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; #自定义响应首部 #添加一个X-Via显示服务器地址 add_header X-Via $server_addr; #添加一个X-Cache显示缓存是否命中 add_header X-Cache $upstream_cache_status; location / { #root指令用于指定虚拟主机的网页根目录,这个目录可以是相对路径,也可以是绝对路径 root html; #index用于设定访问的默认首页地址 index index.html index.htm; #设置转发的目的路径即被代理服务器的地址 proxy_pass http://www.jlu.edu.cn; #设置缓冲区的名字 proxy_cache webserver; #为不同应答设置缓存时间 proxy_cache_valid 200 10m; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html #设置当出现错误的时候跳转的页面 error_page 500 502 503 504 /50x.html; location = /50x.html { root html; proxy_pass http://www.jlu.edu.cn; proxy_cache webserver; proxy_cache_valid 200 10m; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} } # another virtual host using mix of IP-, name-, and port-based configuration # #server { # listen 8000; # listen somename:8080; # server_name somename alias another.alias; # location / { # root html; # index index.html index.htm; # } #} # HTTPS server # #server { # listen 443 ssl; # server_name localhost; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } #} }
Before reloading the above modified file, first create a wenserver folder in /usr/local/nginx:
mkdir webserver
Then run the following Command:
./nginx -s reload
Then enter the IP address and port number of Nginx in the browser to access the web page.
The above introduces the use of Nginx to cache web pages, including aspects of content. I hope it will be helpful to friends who are interested in PHP tutorials.

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

AI Hentai Generator
Generate AI Hentai for free.

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

Guide to Table Border in HTML. Here we discuss multiple ways for defining table-border with examples of the Table Border in HTML.

This is a guide to Nested Table in HTML. Here we discuss how to create a table within the table along with the respective examples.

Guide to HTML margin-left. Here we discuss a brief overview on HTML margin-left and its Examples along with its Code Implementation.

Guide to HTML Table Layout. Here we discuss the Values of HTML Table Layout along with the examples and outputs n detail.

Guide to the HTML Ordered List. Here we also discuss introduction of HTML Ordered list and types along with their example respectively

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Guide to Moving Text in HTML. Here we discuss an introduction, how marquee tag work with syntax and examples to implement.

Guide to HTML onclick Button. Here we discuss their introduction, working, examples and onclick Event in various events respectively.
