Home > Backend Development > PHP Tutorial > Caching web pages using Nginx

Caching web pages using Nginx

WBOY
Release: 2016-07-29 08:51:21
Original
1189 people have browsed it

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;
    #    }
    #}

}
Copy after login

Before reloading the above modified file, first create a wenserver folder in /usr/local/nginx:

mkdir webserver
Copy after login

Then run the following Command:

./nginx -s reload
Copy after login

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.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template