• PHP服务化搭建之nginx动静分离实战


    如有什么问题可以加群交流:647617935

    什么是动静分离

    动静分离:将项目中的CSS,JS,HTML,JPG'。等静态资源和 PHP等动态资源分开处理的一种方式

    动静分离优点

    不同的文件由不同类型的服务器来处理可以使系统架构更加清晰,维护更方便。2.提高服务器响应速度从而增强了用户体验。

    动静分离配置 

     找到的nginx的的安装目录中的CONF目录,打开该目录下的nginx.conf文件进行编辑,新增如下配置:

      1 #user  nobody;
      2 #nginx进程数,建议设置为等于CPU总核心数。
      3 worker_processes  4;
      4 
      5 
      6 #error_log  logs/error.log;
      7 #error_log  logs/error.log  notice;
      8 #error_log  logs/error.log  info;
      9 
     10 #pid        logs/nginx.pid;
     11 
     12 
     13 events {
     14     #nginx工作模式,epoll是linux平台下的高效模式
     15     #use epoll;
     16     #定义nginx每个进程的最大连接数为51200,一般网上都配置65535,根据张宴大神的建议51200即可
     17     worker_connections  51200;
     18 }
     19 
     20 # 设定http服务器
     21 http {
     22     include mime.types; #文件扩展名与文件类型映射表
     23     default_type application/octet-stream; #默认文件类型
     24     
     25     #charset utf-8; #默认编码
     26     server_names_hash_bucket_size 128; #服务器名字的hash表大小
     27     client_header_buffer_size 32k; #上传文件大小限制
     28     large_client_header_buffers 4 64k; #设定请求缓存
     29     client_max_body_size 8m; #设定请求缓存
     30 
     31     log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
     32                       '$status $body_bytes_sent "$http_referer" '
     33                       '"$http_user_agent" "$http_x_forwarded_for"';
     34 
     35     #access_log  logs/access.log  main;
     36 
     37     sendfile on; #开启高效文件传输模式,sendfile指令指定nginx是否调用sendfile函数来输出文件,对于普通应用设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络I/O处理速度,降低系统的负载。注意:如果图片显示不正常把这个改成off。
     38     autoindex on; #开启目录列表访问,合适下载服务器,默认关闭。
     39     tcp_nopush on; #防止网络阻塞
     40     tcp_nodelay on; #防止网络阻塞
     41     keepalive_timeout 65; #长连接超时时间,单位是秒
     42 
     43     #FastCGI相关参数是为了改善网站的性能:减少资源占用,提高访问速度。下面参数看字面意思都能理解。
     44     fastcgi_connect_timeout 300;
     45     fastcgi_send_timeout 300;
     46     fastcgi_read_timeout 300;
     47     fastcgi_buffer_size 64k;
     48     fastcgi_buffers 4 64k;
     49     fastcgi_busy_buffers_size 128k;
     50     fastcgi_temp_file_write_size 128k;
     51 
     52     #gzip模块设置
     53     gzip on; #开启gzip压缩输出
     54     gzip_min_length 1k; #最小压缩文件大小
     55     gzip_buffers 4 16k; #压缩缓冲区
     56     gzip_http_version 1.0; #压缩版本(默认1.1,前端如果是squid2.5请使用1.0)
     57     gzip_comp_level 2; #压缩等级
     58     gzip_types text/plain application/x-javascript text/css application/xml;
     59     #压缩类型,默认就已经包含text/html,所以下面就不用再写了,写上去也不会有问题,但是会有一个warn。
     60     gzip_vary on;
     61     #limit_zone crawler $binary_remote_addr 10m; #开启限制IP连接数的时候需要使用
     62 
     63     server {
     64         listen       80;
     65         server_name  localhost;
     66 
     67         #charset koi8-r;
     68 
     69         #access_log  logs/host.access.log  main;
     70 
     71         location / {
     72             root   static;
     73             index  index.html index.htm;
     74             proxy_redirect off;
     75             proxy_set_header X-Real-IP $remote_addr;
     76             #后端的Web服务器可以通过X-Forwarded-For获取用户真实IP
     77             proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
     78             #以下是一些反向代理的配置,可选。
     79             proxy_set_header Host $host;
     80             client_max_body_size 10m; #允许客户端请求的最大单文件字节数
     81             client_body_buffer_size 128k; #缓冲区代理缓冲用户端请求的最大字节数,
     82             proxy_connect_timeout 90; #nginx跟后端服务器连接超时时间(代理连接超时)
     83             proxy_send_timeout 90; #后端服务器数据回传时间(代理发送超时)
     84             proxy_read_timeout 90; #连接成功后,后端服务器响应时间(代理接收超时)
     85             proxy_buffer_size 4k; #设置代理服务器(nginx)保存用户头信息的缓冲区大小
     86             proxy_buffers 4 32k; #proxy_buffers缓冲区,网页平均在32k以下的设置
     87             proxy_busy_buffers_size 64k; #高负荷下缓冲大小(proxy_buffers*2)
     88             proxy_temp_file_write_size 64k;
     89             #设定缓存文件夹大小,大于这个值,将从upstream服务器传
     90         }
     91         
     92         #本地动静分离反向代理配置
     93         #静态文件交给nginx处理
     94         #所有静态文件由nginx直接读取不经过tomcat或resin
     95         location ~ .*.(htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma)$ { 
     96             root static;
     97             expires 15d; 
     98         }
     99         
    100         #JS和CSS缓存时间设置
    101         location ~ .*.(js|css)?$ {
    102             root static;
    103             expires 1h;
    104         }
    105         
    106         #设定查看Nginx状态的地址
    107         #location /NginxStatus {
    108             #stub_status on;
    109             #access_log on;
    110             #auth_basic "NginxStatus";
    111             #auth_basic_user_file conf/htpasswd;
    112             #htpasswd文件的内容可以用apache提供的htpasswd工具来产生。
    113         #}
    114         
    115         #所有php的页面均交由php-fpm处理
    116         location ~ .php(.*)$ {
    117           fastcgi_pass   127.0.0.1:9000;
    118                   fastcgi_index  index.php;
    119                   fastcgi_split_path_info  ^((?U).+.php)(/?.+)$;
    120                   fastcgi_param  SCRIPT_FILENAME      
    121                   $document_root$fastcgi_script_name;
    122                   fastcgi_param  PATH_INFO  $fastcgi_path_info;
    123                   fastcgi_param  PATH_TRANSLATED  
    124                   $document_root$fastcgi_path_info;
    125                   include        fastcgi_params;
    126         }
    127         
    128 
    129         #error_page  404              /404.html;
    130 
    131         # redirect server error pages to the static page /50x.html
    132         #
    133         error_page   500 502 503 504  /50x.html;
    134         location = /50x.html {
    135             root   html;
    136         }
    137         
    138     }
    139 
    140 
    141 
    142 
    143 
    144     # HTTPS server
    145     #
    146     #server {
    147     #    listen       443 ssl;
    148     #    server_name  localhost;
    149 
    150     #    ssl_certificate      cert.pem;
    151     #    ssl_certificate_key  cert.key;
    152 
    153     #    ssl_session_cache    shared:SSL:1m;
    154     #    ssl_session_timeout  5m;
    155 
    156     #    ssl_ciphers  HIGH:!aNULL:!MD5;
    157     #    ssl_prefer_server_ciphers  on;
    158 
    159     #    location / {
    160     #        root   html;
    161     #        index  index.html index.htm;
    162     #    }
    163     #}
    164 
    165 }

    动静分离测试  

    首先在 static 目录中存放一些静态资源文件,如下图所示:

    然后切换 cmd路径 到nginx目录下重启nginx服务器nginx -s reload,打开火狐浏览器访问网址:http://localhost/js/XX.js ,F12打开调式工具,可以看到文件的存储时间。(大家自行找找看,在响应头哦)

    观察后大家可以发现,静态资源文件直接从磁盘获取,响应头有Cache-Control字段,静态资源的请求时间均为0ms。 (注:项目某php文件中需要引用该js文件,可以直接这样写,如下:)

    <script type="text/javascript" src="/js/xx.js"></script>

      

  • 相关阅读:
    【PHP】新浪、淘宝的地区 API调用
    wdcp/wdlinux 常用工具及命令集
    wdcp/wdlinux一键包的php5.3版本添加Zend.so 和Soap.so
    WDCP一些常用的一健安装包可选安装组件
    WDCP安装memcached
    WDCP控制面板安装卸载
    linux添加环境变量
    Linux常用命令大全
    [译]git commit
    [译]git add
  • 原文地址:https://www.cnblogs.com/winner192/p/11989469.html
Copyright © 2020-2023  润新知