目录php
Nginx安装方式:html
yum源安装mysql
目录结构:nginx
源码包安装c++
目录结构:sql
Nginx中支持PHP数据库
Nginx配置反向代理服务器centos
正常代理服务器
Nginx是一个高性能的HTTP和反向代理服务器,该程序由俄罗斯Rambler.ru 站点开发,Nginx由于性能稳定、低系统资源消耗而闻名。默认监听端口: tcp / 80
nginx安装包官网下载地址:http://nginx.org/en/download.html
yum安装会比源码包编译安装简单的多,默认会安装许多模块,但缺点是若是之后想安装第三方模块那就没办法了。
可是默认的 yum 源不少都没有nginx包的,想安装nginx,咱们能够在 /etc/yum.repos.d/ 下新建一个 nginx.repo 文件,而后加入下面的内容 (Redhat 7 和 Centos 7 中适用)
[nginx] name=nginx repo baseurl=http://nginx.org/packages/centos/7/$basearch/ gpgcheck=0 enabled=1 //若是是Centos6的系统,把baseurl中的7换成6便可
而后 yum -y install nginx 安装便可。
程序可执行文件: /usr/sbin/nginx
主要配置文件: /etc/nginx/conf.d/default.conf
网页主页目录:/usr/share/nginx/html/
日志文件目录:/var/log/nginx/
控制服务: systemctl start / stop / restart / status nginx
访问服务器,若是看到下面页面,说明nginx服务器搭建完成!
安装完成后,nignx默认安装在 /usr/local/ 目录下
程序可执行文件: /usr/local/nginx/sbin/nginx
配置文件: /usr/local/nginx/conf/nginx.conf
网页主页目录:/usr/loca/nginx/html/
日志文件目录:/usr/local/nginx/log/
可是,当你去 /usr/local/nginx/sbin/ 下执行 ./nginx 命令运行程序时,有可能会报错
[root@Centos sbin]# ./nginx ./nginx: error while loading shared libraries: libpcre.so.1: cannot open shared object file: No such file or directory
报这个错是由于咱们的 libpcre 的相关库文件在 /usr/loca/lib/ 下,而nginx默认读的是 /lib64/ 下的。因此,咱们只须要添加一个软连接便可解决: ln -s /usr/local/lib/libpcre.* /lib64/
解决了上面的问题,咱们就能够直接启动nignx了:咱们可使用这个命令启动nginx : /usr/local/nginx/sbin/nginx ,也能够去 /usr/local/nginx/sbin/ 目录下直接 nginx 启动。可是这样未免太麻烦了。因而,咱们也能够建立一个软连接: ln -s /usr/local/nginx/sbin/nginx /usr/sbin/nginx ,这样,不管咱们到了哪一个位置,均可以直接 nginx 启动。而后中止的话,能够用 nginx -s stop 。
ningx中支持php的包为 php-fpm (php fastcgi process manager) , PHP-FPM提供了更好的PHP进程管理方式,能够有效控制内存和进程、能够平滑重载PHP配置。
安装php-fpm: yum -y install php-fpm
而后修改nginx配置文件: /etc/nginx/conf.d/default.conf
location / { //默认全部形式的后缀都由这里处理 root /usr/share/nginx/html; //主页目录 index index.php index.html index.htm; //在这里加上 index.php } // 把下面这几行的注释给取消,而后修改 location ~ \.php$ { //将后缀为 .php 的由这里处理 root /usr/share/nginx/html; //主页目录 fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; //这里须要修改 include fastcgi_params; }
修改完nginx配置文件后,开启php-fpm: systemctl start php-fpm
而后让nginx从新加载配置文件: nginx -s reload
php的配置文件: /etc/php.ini 是用来控制php解析器
/etc/php-fpm.conf是控制php-fpm守护进程的
yum -y install php-mysql
将外界客户端发来的请求,转发给真正提供服务的服务器。
修改nginx配置文件:/etc/nginx/nginx.conf ,在最后的 http{ } 中加入如下内容
// 下面这些代码的意思是将本机80号端口接受来的消息转发给 192.168.10.10 的 80 号端口 upstream www.xie.com{ //这里的域名随便填,可是要和下面的对应 server 192.168.10.10:80; //真正提供服务的服务器的ip地址和端口 } server{ listen 80; // 监听80号端口发过来的消息 location /{ proxy_pass http://www.xie.com; index index.html index.php; } }
//这些代码的意思是将80端口接收到的信息转发给 192.168.10.10的80端口,而将接受到的 8080端口的信息转发给 192.168.10.20的8080端口 upstream www.xie.com{ server 192.168.10.10:80; } upstream www.xie2.com{ server 192.168.10.20:8080; } server{ listen 80; location /{ proxy_pass http://www.xie.com; index index.html index.php; } } server{ listen 8080; location /{ proxy_pass http://www.xie2.com; index index.html index.jsp; } }
1:轮询:每一个请求按时间顺序逐一分配到不一样的后端服务器,若是后端服务器down掉,能自动剔除 upstream www.xie.com{ server 192.168.10.10:80; server 192.168.10.20:80; } server{ listen 80; location /{ proxy_pass http://www.xie.com; index index.html index.php index.jsp; } } 2:ip_hash:每一个请求按访问ip的hash结果分配,这样每一个访客固定访问一个后端服务器,能够解决session的问题。 upstream www.xie.com{ ip_hash; server 192.168.10.10:80; server 192.168.10.20:80; } server{ listen 80; location /{ proxy_pass http://www.xie.com; index index.html index.php index.jsp; } } 3:weight:指定轮询概率,weight和访问比率成正比,用于后端服务器性能不均的状况。 upstream www.xie.com{ server 192.168.10.10:80 weight=10; server 192.168.10.20:80 weight=20; } server{ listen 80; location /{ proxy_pass http://www.xie.com; index index.html index.php index.jsp; } } 4: fair : 按后端服务器的响应时间来分配请求,响应短的服务器优先分配 upstream www.xie.com{ server 192.168.10.10:80 weight=10; server 192.168.10.20:80 weight=20; fair; } server{ listen 80; location /{ proxy_pass http://www.xie.com; index index.html index.php index.jsp; } }
以上的反向代理的工做原理,都是根据监听不一样端口,而后将流量转发到真正服务器的不一样端口。
还有一种反向代理是监听80端口,而后根据请求的页面的不一样后缀(.php / .jsp / 等等),来转发给不一样的端口进行解析。
server { listen 80; server_name localhost; location / { root /usr/share/nginx/html; index index.php index.html index.htm; } location ~ \.php$ { //当请求的后缀是以 .php结尾的话,将流量转发给本地的800端口 proxy_pass http://127.0.0.1:800; } location ~ \.jsp$ { //当请求的后缀是以 .jsp结尾的话,将流量转发给本地的8080端口 proxy_pass http://127.0.0.1:8080; } location ~ \.(jpg|png)$ { //当请求的后缀是以 .jpg或.png 结尾的话,则请求 /img 目录下 root /img; } }