NGINX相关知识
---------------------------------------
nginx 和 tengine(淘宝) 类似,
1.查看网站的head头:
curl -I www.51cto.com
2.安装nginx步骤:
一。先安装pcre依赖,rewrite需要用:
yum install pcre pcre-devel openssl-devel
二。到http://nginx.org官网下载稳定版本:
useradd nginx -s /sbin/nologin -M tar -zxvf nginx-1.10.2.tar.gz ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module make && make install3.查看安装时的参数:
./sbin/nginx -V
4.配置文件:
worker_processes 1; 线程数,配置成CPU核数 worker_connections 1024; 每个worker允许的最大并发数默认虚拟主机配置:
server { listen 80; server_name bbs.wmj.com; access_log logs/www_access.log main; location / { root html/bbs; index index.html index.htm; } }5.生效配置:
./sbin/nginx -t 测试配置文件是否正确
./sbin/nginx -s reload 加载配置文件
6.使用include优化配置文件:
include vhost/*.conf;7.查看nginx的状态信息:
vim conf/vhost/status.conf
server{ listen 80; server_name status.wmj.com; location / { stub_status on; access_log off; } }
8.error日志配置:
vim conf/nginx.conf
error_log logs/error.log error; 可以放在 http,server 头里面9.访问日志配置:
vim conf/nginx.conf
log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for" "$request_time"'; vim conf/vhost/bbs.conf
access_log logs/bbs_access.log main;
10.重定向rewrite的使用:
server { listen 80; server_name rewrite.wmj.com; access_log logs/www_access.log main; rewrite ^/(.*) http://www.wmj.com/$1 permanent; } 将"http://rewrite.wmj.com/wmj.html"重定向到"http://www.wmj.com/wmj.html" permanent: 表示301重定向,没有的话是302
二. LNMP架构
----------------------------------
1.FastCGI的工作原理:
2.安装php-5.5.38:
先到官网上面下载php包:http://cn.php.net/releases/
安装相关依赖:
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo (加载阿里云repo源) yum install zlib-devel libxml2-devel libjpeg-devel libmcrypt-devel yum install freetype-devel libpng-devel gd-devel curl-devel libxslt-devel yum -y install libmcrypt-devel mhash mhash-devel mcrypt wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.14.tar.gz ./configure --prefix=/usr/local/libiconv make && make install开始安装php:
tar xf php-5.5.38.tar.gz cd php-5.5.38 ./configure \ --prefix=/usr/local/php5.5.38 \ --with-mysql=mysqlnd \ --with-iconv-dir=/usr/local/libiconv \ --with-freetype-dir \ --with-jpeg-dir \ --with-png-dir \ --with-zlib \ --with-libxml-dir=/usr \ --enable-xml \ --disable-rpath \ --enable-bcmath \ --enable-shmop \ --enable-sysvsem \ --enable-inline-optimization \ --with-curl \ --enable-mbregex \ --enable-fpm \ --enable-mbstring \ --with-mcrypt \ --with-gd \ --enable-gd-native-ttf \ --with-openssl \ --with-mhash \ --enable-pcntl \ --enable-sockets \ --with-xmlrpc \ --enable-soap \ --enable-short-tags \ --enable-static \ --with-xsl \ --with-fpm-user=nginx \ --with-fpm-group=nginx \ --enable-ftp make make install3.复制默认的配置文件:
cp php.ini-production /usr/local/php5.5.38/lib/php.ini
cd /usr/local/php5.5.38/etc/
cp php-fpm.conf.default php-fpm.conf
4.修改配置文件:
vim php-fpm.conf
log_level = error rlimit_files = 65535 pm.max_children = 1024 pm.start_servers = 16 pm.min_spare_servers = 5 pm.max_spare_servers = 205.启动Php:
/usr/local/php5.5.38/sbin/php-fpm
6.配置php的nginx虚拟主机:
vim vhost/blog.conf
server { listen 80; server_name blog.wmj.com; access_log logs/blog_access.log main; root html/blog; location / { index index.html index.htm; } location ~ .*\.(php|php5)?$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; include fastcgi.conf; } } ~ 7.php测试:vim ../../html/blog/phpinfo.php
<?php phpinfo(); ?> 三. Nginx 的负载均衡: ------------------------------- 1.调度算法种类: 轮询(rr), 权重轮询(wrr),ip_hash(会话保持) 2.配置nginx配置文件: upstream backend { server 172.16.1.214:80 weight=5; server 172.16.1.210:80 max_fails=2 fail_timeout=30s; server 172.16.1.215:80 backup; } server { location / { proxy_pass http://backend; } }3.将请求的域名和客户端的IP传送到后端: server { location / { proxy_pass http://backend; proxy_set_header Host $host; #后端会执行前端访问的域名 proxy_set_header X-Forwarded-For $remote_addr; #把客户端的IP传给后端 proxy_set_header X-Real-IP $remote_addr; } } 4. 生产环境优化反向代理proxy参数配置: proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; { proxy_connect_timeout 90; proxy_send_timeout 90; proxy_read_timeout 90; proxy_buffer_size 4k; proxy_buffers 4 32k; proxy_busy_buffers_size 64k; proxy_temp_file_write_size 64k;} 大括号里面的根据实际情况调整,也可以不设置用默认 将它保存成文件,然后用include导入。 server { location / { proxy_pass http://backend; include proxy.conf; } } 5.根据扩展名来实现动静分离: location ~ .*.(gif|jpg|png|bmp)$ { proxy_pass http://image; include proxy.conf; }http://www.jb51.net/books/24955.html
http://www.cnblogs.com/zhangrumingbj/p/3887693.html