什么是nginx

  • 最主要最基础的功能是发布程序
  • nginx是高性能的HTTP和反向代理服务器,所以还可以实现负载均衡
  • 收发邮件

负载均衡

当访问量多的时候,nginx可以把请求分给服务器集群,加快应答速度,避免服务器崩溃。

优缺点

  • 优点:高并发、部署简单、内存消耗少、成本低等
  • 缺点:rewrite功能不强,模块没有Apache多

centos中配置

1.安装支持,不然下面安装的时候会报错

yum -y install pcre*

yum -y install openssl*

2.下载nginx

wget http://nginx.org/download/nginx-1.7.8.tar.gz

3.解压

tar -zxvf nginx-1.7.8.tar.gz

4.编译安装

cd nginx-1.7.8

./configure –prefix=/usr/local/nginx-1.5.1 \ –with-http_ssl_module –with-http_spdy_module \ –with-http_stub_status_module –with-pcre

5.安装

make

make install

6.启动/重启/关闭

/usr/local/nginx-1.5.1/sbin/nginx

/usr/local/nginx-1.5.1/sbin/nginx -s reload

/usr/local/nginx-1.5.1/sbin/nginx -s stop

nginx.conf

工作衍生进程数,最合适的是内核数或两倍

worker_processes 1

设置最大连接数

events {
worker_connections 1024;
}

http

  • gzip on 开启gzip压缩,传输过程中压缩后的文件是原来的30%
  • charset koi8-r 设置字符编码

基本格式

1
2
3
4
5
6
7
8
9
worker_processes 1;
events{
worker_connections 1024;
}
http{
server{...}
server{...}
...
}

反向代理解决跨域问题

从我的html访问到nginx反向代理的api,先把http://114.215.95.111/api这么一个接口用来做反向代理的入口,

修改nginx.conf:

1
2
3
4
location /api {
rewrite ^.+api/?(.*)$ /$1 break;
proxy_pass http://www.runoob.com/;
}