首页 文章 精选 留言 我的

精选列表

搜索[lamp],共675篇文章
优秀的个人博客,低调大师

LAMP架构(apache访问日志不记录静态文件,静态元素过期时间,apache日志切割)

一、apache访问日志不访问静态文件 浏览器打开后按f12会会出现一个界面,点network,会出现很多请求,访问日志里会记载,有些静态的图片或者文件也会记载,太浪费磁盘空间和磁盘io。所以我们要让日志不记录静态文件 [root@abc ~]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf SetEnvIfRequest_URI ".*\.gif$" img SetEnvIfRequest_URI ".*\.jpg$" img SetEnvIfRequest_URI ".*\.png$" img SetEnvIfRequest_URI ".*\.bmp$" img SetEnvIfRequest_URI ".*\.swf$" img SetEnvIfRequest_URI ".*\.js$" img SetEnvIfRequest_URI ".*\.css$" img 定义这些以gif.jgp,png,bmp.swf.js.css结尾的文件为env。 在CustomLog "logs/111.com-access_log" combined 这一行的最后加上env=!img,表示把除了img的文件记录到日志里。 检查语法错误并退出。 实验: curl -x127.0.0.1:80 111.com/sadaddas.png1 curl -x127.0.0.1:80 111.com/sadaddas.png 访问png1和png两个文件,看看日志会记录哪个? 结果为:png不会被记录,png1会被记录, 或者上传一张以png结尾的图片在目录里,用浏览器访问,结果还是没有日志 二、apache访问日志切割 日志一直记录总有一天会把整个磁盘沾满,所以有必要让它自动切割,并删除老的日志文件。保证磁盘永远写不满,且更方便的管理日志 vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf 修改 ustomLog "logs/111.com-access_log" combined env=!img 为: CustomLog "|/usr/local/apache2.4/bin/rotatelogs -l logs/111.com-access_log" combined env=!img rotatelogs是apache自带的日志切割工具。-l是为了让它以当前系统时间为准,如果不指定。他会以utc时间为基准。 而且根据时间日期还要让他文件有不同的名字。修改111.com-access_log为111.com-access%Y%m%d.log %Y%m%d分别表示年月日. 还要规定什么时间去切割。一天是86400秒,日志名后面加上86400 检查语法错误并退出。 实验: 访问一个不是静态文件的网址内容 [root@abc ~]# curl -x127.0.0.1:80 111.com/dsd.png1 发现日志目录里多了一个以当天时间为名的日志文件,里面记录的则是刚Curl的 [root@abc ~]# ls /usr/local/apache2.4/logs/ 111.com-access20171126.log111.com-access_log 111.com-error_log abc.com abc.com-access_log access_log error_log httpd.pid [root@abc ~]# cat /usr/local/apache2.4/logs/111.com-access20171126.log 127.0.0.1 - - [26/Nov/2017:15:28:31 +0800] "GET HTTP://111.com/dsd.png1 HTTP/1.1" 404 206 "-" "curl/7.29.0" 三、静态元素过期时间 配置静态文件元素过期时间 浏览器访问网站图片时会把静态文件缓存在本地电脑里。这样下次访问就不用再去远程下载了。 实验: 可以在目录里放一个图片,然后用浏览器去访问,且访问之前先F12打开管理页面,点开Network 可以发现,第一次访问时,状态码为200,表示正正常 第二次访问时,状态码为304 Not Modified 表示本地有缓存,且没有修改,所以不需要从新下载。 这样可以节省带宽,但是没有规定失效日期且什么时候清理缓存。 修改: vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf 添加: <IfModulemod_expires.c> ExpiresActive on //打开该功能的开关 ExpiresByType image/gif "access plus 1 days" ExpiresByType image/jpeg "access plus 24 hours" ExpiresByType image/png "access plus 24 hours" ExpiresByType text/css "now plus 2 hour" ExpiresByType application/x-javascript "now plus 2 hours" ExpiresByType application/javascript "now plus 2 hours" ExpiresByType application/x-shockwave-flash "now plus 2 hours" ExpiresDefault "now plus 0 min" </IfModule> expire是apache的一个模块。设置过期时间 检查apache有没有这个模块 [root@abc ~]# /usr/local/apache2.4/bin/apachectl -M |grep expire 如果没有,则需要到主配置文件里修改 搜索expire 在109行,把前面的注释符去掉 保存退出 检查语法错误并重启服务 [root@abc ~]# /usr/local/apache2.4/bin/apachectl -M |grep expire expires_module (shared) apache加载了这个模块。 实验: ctrl+f5可以强制刷新,把本地缓存清空 再访问图片是状态码显示为200,且下面有cache-control,过期时间为86400秒,也就是一天,还会告诉你过期日期的时间expires,但是时区是GMT. [root@abc 111.com]# curl -x127.0.0.1:80 111.com/1.png -I HTTP/1.1 200 OK Date: Sun, 26 Nov 2017 08:04:11 GMT Server: Apache/2.4.28 (Unix) PHP/5.6.30 Last-Modified: Sun, 26 Nov 2017 07:07:52 GMT ETag: "7913-55edd71b4a200" Accept-Ranges: bytes Content-Length: 30995 Cache-Control: max-age=86400 Expires: Mon, 27 Nov 2017 08:04:11 GMT Content-Type: image/png 本文转自 小新锐 51CTO博客,原文链接:http://blog.51cto.com/13407306/2053108,如需转载请自行联系原作者

优秀的个人博客,低调大师

LAMP架构(nginx安装,默认虚拟主机,用户认证,域名重定向,nginx配置文件详解)

一、安装nginx [root@lnmp conf]# wgethttp://nginx.org/download/nginx-1.8.0.tar.gz [root@lnmp conf]# tar zxvf nginx-1.8.0.tar.gz [root@lnmp conf]# cd nginx-1.8.0 [root@lnmp conf]# ./configure --prefix=/usr/local/nginx [root@lnmp conf]# make && make install [root@lnmp conf]# vim /etc/init.d/nginx #!/bin/bash # chkconfig: - 30 21 # description: http service. # Source Function Library . /etc/init.d/functions # Nginx Settings NGINX_SBIN="/usr/local/nginx/sbin/nginx" NGINX_CONF="/usr/local/nginx/conf/nginx.conf" NGINX_PID="/usr/local/nginx/logs/nginx.pid" RETVAL=0 prog="Nginx" start() { echo -n $"Starting $prog: " mkdir -p /dev/shm/nginx_temp daemon $NGINX_SBIN -c $NGINX_CONF RETVAL=$? echo return $RETVAL } stop() { echo -n $"Stopping $prog: " killproc -p $NGINX_PID $NGINX_SBIN -TERM rm -rf /dev/shm/nginx_temp RETVAL=$? echo return $RETVAL } reload() { echo -n $"Reloading $prog: " killproc -p $NGINX_PID $NGINX_SBIN -HUP RETVAL=$? echo return $RETVAL } restart() { stop start } configtest() { $NGINX_SBIN -c $NGINX_CONF -t return 0 } case "$1" in start) start ;; stop) stop ;; reload) reload ;; restart) restart ;; configtest) configtest ;; *) echo $"Usage: $0 {start|stop|reload|restart|configtest}" RETVAL=1 esac exit $RETVAL [root@lnmp conf]# chmod 755 /etc/init.d/nginx [root@lnmp conf]# chkconfig --add nginx [root@lnmp conf]# chkconfig nginx on [root@lnmp conf]# mv nginx.conf nginx.conf.bak [root@lnmp conf]# vim /usr/local/nginx/conf/nginx.conf user nobody nobody; (启动nginx的用户) worker_processes 2; (定义子进程) error_log /usr/local/nginx/logs/nginx_error.log crit; (错误日志) pid /usr/local/nginx/logs/nginx.pid; (pid位置) worker_rlimit_nofile 51200; (最多打开多少文件) events { use epoll; worker_connections 6000; (进程最多多少连接) } http { include mime.types; default_type application/octet-stream; server_names_hash_bucket_size 3526; server_names_hash_max_size 4096; log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_local]' ' $host "$request_uri" $status' ' "$http_referer" "$http_user_agent"'; sendfile on; tcp_nopush on; keepalive_timeout 30; client_header_timeout 3m; client_body_timeout 3m; send_timeout 3m; connection_pool_size 256; client_header_buffer_size 1k; large_client_header_buffers 8 4k; request_pool_size 4k; output_buffers 4 32k; postpone_output 1460; client_max_body_size 10m; client_body_buffer_size 256k; client_body_temp_path /usr/local/nginx/client_body_temp; proxy_temp_path /usr/local/nginx/proxy_temp; fastcgi_temp_path /usr/local/nginx/fastcgi_temp; fastcgi_intercept_errors on; tcp_nodelay on; gzip on; gzip_min_length 1k; gzip_buffers 4 8k; gzip_comp_level 5; gzip_http_version 1.1; gzip_types text/plain application/x-javascript text/css text/htm application/xml; server (http服务) { listen 80; (监听80端口) server_name localhost; (设置域名) index index.html index.htm index.php; (设置主页) root /usr/local/nginx/html; (设置访问主目录) location ~ \.php$ (定义php解析) { include fastcgi_params; fastcgi_pass unix:/tmp/php-fcgi.sock; #fastcgi_pass 127.0.0.1:9000; (和上面一行的意思相同,只是不同的写法,监听127.0.0.1:9000) fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name; } } } [root@lnmp conf]# /usr/local/nginx/sbin/nginx -t (检查语法错误) [root@lnmp conf]# /etc/init.d/nginx start (启动nginx) [root@lnmp conf]# netstat -lntp |grep 80 (查看80端口) [root@lnmp conf]# ps aux |grep nginx (查看nginx服务,可看到2个work子进程) 二、nginx默认虚拟主机 Nginx默认主机: [root@lnmp ~]# vim /usr/local/nginx/conf/nginx.conf (删除server及下面的,在http最后添加) include vhost/*.conf; (指定虚拟主机目录,并读取以.conf结尾的文件) :wq退出保存 [root@lnmp ~]#mkdir /usr/local/nginx/conf/vhost (创建虚拟主机目录) [root@lnmp ~]#vim aaa.com.conf (创建虚拟主机配置文件并添加以下内容:) server { listen 80default_server; (红色的字表示设置这个虚拟主机为默认虚拟主机) server_name aaa.com; index index.html index.htm index.php; root /data/wwwroot/default; } [root@lnmp vhost]# mkdir -p /data/wwwroot/default/ (创建虚拟主机的访问目录) [root@lnmp vhost]# echo "This is a default site." >/data/wwwroot/default/index.html (编写虚拟主机主页) [root@lnmp vhost]# /usr/local/nginx/sbin/nginx -t (检查配置文件语法错误) [root@lnmp vhost]# /usr/local/nginx/sbin/nginx -s reload (重新加载配置文件) [root@lnmp vhost]# curl localhost (curl本机,发现到达nginx的虚拟主机主页) This is a default site. !!:还有一个需要注意的是,如果不加红色字体的字段,再找server时会根据文件名排序,比如:aaa.com.cnf和bbb.com.cnf,aaa肯定是在前,所以aaa.com.cnf是默认虚拟主机 三、Nginx用户认证 nginx用户认证 用到了之前httpd的htpasswd功能。 [root@lnmp ~]# vim /usr/local/nginx/conf/vhost/test.com.conf (创建一个虚拟主机) server { listen 80; server_name test.com; index index.html index.htm index.php; root /data/wwwroot/test.com; location / { auth_basic "Auth"; (定义用户认证的名字) auth_basic_user_file /usr/local/nginx/conf/htpasswd (定义用户名密码文件) } } 因为要使用到httpd的htpasswd功能,则需要安装httpd,可以直接yum安装,直接敲htpasswd命令, [root@lnmp ~]# htpasswd -c /usr/local/nginx/conf/htpasswd lty (c是生成用户文件,若要添加则不需要,否则会覆盖原文件) New password: Re-type new password: Adding password for user lty 检查语法错误,并且重新加载配置文件:(如果配置文件出现错误,reload不会使错误的配置文件生效) [root@lnmp ~]# /usr/local/nginx/sbin/nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful [root@lnmp ~]# /usr/local/nginx/sbin/nginx -s reload 检测: [root@lnmp ~]# curl -x127.0.0.1:80 test.com -I (不加用户发现401,需要用户认证) HTTP/1.1 401 Unauthorized Server: nginx/1.8.0 Date: Thu, 14 Dec 2017 04:15:02 GMT Content-Type: text/html Content-Length: 194 Connection: keep-alive WWW-Authenticate: Basic realm="Auth" [root@lnmp ~]# curl -ulty:westos -x127.0.0.1:80 test.com (-u指定用户和密码后,返回值) test.com 1.需求;访问一个目录或者文件时,才需要用户认证。 实现: [root@lnmp ~]# vim /usr/local/nginx/conf/vhost/test.com.conf server { listen 80; server_name test.com; index index.html index.htm index.php; root /data/wwwroot/test.com; location /admin (用户认证时加上admin目录) { auth_basic "Auth"; auth_basic_user_file /usr/local/nginx/conf/htpasswd; } } 检测语法错误并且重新加载配置文件: [root@lnmp ~]# /usr/local/nginx/sbin/nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful [root@lnmp ~]# /usr/local/nginx/sbin/nginx -s reload 检测: [root@lnmp ~]# curl -x127.0.0.1:80 test.com (访问test.com时不需要密码也可以返回值) test.com [root@lnmp ~]# curl -x127.0.0.1:80 test.com/admin (访问test.com下的admin时,401需要用户认证) <html> <head><title>401 Authorization Required</title></head> <body bgcolor="white"> <center><h1>401 Authorization Required</h1></center> <hr><center>nginx/1.8.0</center> </body> </html> 需求,访问test.com下的1.php需要用户认证, [root@lnmp ~]# vim /usr/local/nginx/conf/vhost/test.com.conf server { listen 80; server_name test.com; index index.html index.htm index.php; root /data/wwwroot/test.com; location /admin/1.php (这里修改匹配到1.php) { auth_basic "Auth"; auth_basic_user_file /usr/local/nginx/conf/htpasswd; 检测语法错误并且重新加载配置文件: [root@lnmp ~]# /usr/local/nginx/sbin/nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful [root@lnmp ~]# /usr/local/nginx/sbin/nginx -s reload 检测: [root@lnmp ~]# curl -x127.0.0.1:80 test.com/admin/1.php (不加用户密码访问发现401) <html> <head><title>401 Authorization Required</title></head> <body bgcolor="white"> <center><h1>401 Authorization Required</h1></center> <hr><center>nginx/1.8.0</center> </body> </html> [root@lnmp ~]# curl -ulty:westos -x127.0.0.1:80 test.com/admin/1.php (加用户密码访问则正常返回) touch file.php 四、nginx域名重定向 httpd配置文件里server_name后面不支持写多个域名,就算写了多个,也默认识别第一个 nginx的配置文件server_name后面则支持写多个域名, [root@lnmp ~]# vim /usr/local/nginx/conf/vhost/test.com.conf server { listen 80; server_name test.com test1.com test2.com; (server_name后跟多个域名) index index.html index.htm index.php; root /data/wwwroot/test.com; if ($host != 'test.com' ) { (如果域名不是test.com) rewrite ^/(.*)$ http://test.com/$1 permanent; (rewrite到test.com,permanent301报错 redirect302报错) 检查语法错误并且重新加载配置文件: [root@lnmp ~]# /usr/local/nginx/sbin/nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful [root@lnmp ~]# /usr/local/nginx/sbin/nginx -s reload 检测: [root@lnmp ~]# curl -x127.0.0.1:80 test1.com/admin/1.php -I (访问test1时,提示301,跳转到test.comx下) HTTP/1.1 301 Moved Permanently Server: nginx/1.8.0 Date: Thu, 14 Dec 2017 05:03:32 GMT Content-Type: text/html Content-Length: 184 Connection: keep-alive Location: http://test.com/admin/1.php [root@lnmp ~]# curl -x127.0.0.1:80 test2.com/admin/1.php -I (访问test2时,提示301,跳转到test.comx下) HTTP/1.1 301 Moved Permanently Server: nginx/1.8.0 Date: Thu, 14 Dec 2017 05:03:38 GMT Content-Type: text/html Content-Length: 184 Connection: keep-alive Location: http://test.com/admin/1.php 五、Nginx配置文件详解 http://www.ha97.com/5194.html https://my.oschina.net/duxuefeng/blog/34880 本文转自 小新锐 51CTO博客,原文链接:http://blog.51cto.com/13407306/2057083,如需转载请自行联系原作者

资源下载

更多资源
Mario

Mario

马里奥是站在游戏界顶峰的超人气多面角色。马里奥靠吃蘑菇成长,特征是大鼻子、头戴帽子、身穿背带裤,还留着胡子。与他的双胞胎兄弟路易基一起,长年担任任天堂的招牌角色。

Spring

Spring

Spring框架(Spring Framework)是由Rod Johnson于2002年提出的开源Java企业级应用框架,旨在通过使用JavaBean替代传统EJB实现方式降低企业级编程开发的复杂性。该框架基于简单性、可测试性和松耦合性设计理念,提供核心容器、应用上下文、数据访问集成等模块,支持整合Hibernate、Struts等第三方框架,其适用范围不仅限于服务器端开发,绝大多数Java应用均可从中受益。

Rocky Linux

Rocky Linux

Rocky Linux(中文名:洛基)是由Gregory Kurtzer于2020年12月发起的企业级Linux发行版,作为CentOS稳定版停止维护后与RHEL(Red Hat Enterprise Linux)完全兼容的开源替代方案,由社区拥有并管理,支持x86_64、aarch64等架构。其通过重新编译RHEL源代码提供长期稳定性,采用模块化包装和SELinux安全架构,默认包含GNOME桌面环境及XFS文件系统,支持十年生命周期更新。

WebStorm

WebStorm

WebStorm 是jetbrains公司旗下一款JavaScript 开发工具。目前已经被广大中国JS开发者誉为“Web前端开发神器”、“最强大的HTML5编辑器”、“最智能的JavaScript IDE”等。与IntelliJ IDEA同源,继承了IntelliJ IDEA强大的JS部分的功能。

用户登录
用户注册