一、nginx 基本介绍和配置文件
1、centos 安装 nginx
- 注意centos7 默认是没有 nginx 的软件源的,yum 安装之前先要添加软件源
rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
- yum安装之后,使用如下的命令可以启动停止和重新加载 nginx 的配置文件
nginx
nginx -s quit
nginx -s reload
2、基本介绍
nginx 是什么
- nginx是一个HTTP服务器,也是一个反向代理服务器
- 相比于apache的多进程多线程的并发模型,而nginx是基于事件的异步IO的并发模型,性能更好
通过源码编译安装指定安装的一些配置项
- ./configure ---- make ---- make install
./configure \
--user=nginx \
--group=nginx \
--prefix=/etc/nginx \
--sbin-path=/usr/sbin/nginx \
--conf-path=/etc/nginx/nginx.conf \
--pid-path=/var/run/nginx.pid \
--lock-path=/var/run/nginx.lock \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--with-http_gzip_static_module \
--with-http_stub_status_module \
--with-http_ssl_module \
--with-pcre \
--with-file-aio \
--with-http_realip_module \
--without-http_scgi_module \
--without-http_uwsgi_module \
--without-http_fastcgi_module
3、配置文件说明
- centos7环境下默认安装 nginx ,未修改configure 配置
/etc/nginx/nginx.conf
- 这个配置分为两个block(events 和 http)
- 没有被 {} 包裹的部分为全局配置,如 worker_processes 1,设置工作进程(子进程)数为 1
- events {} 为 nginx 连接配置的模块,如 worker_connections 1024; 设置每一个子进程最大允许连接 1024 个连接
- http {} 为 nginx http 核心配置模块
- server {} 为虚拟主机配置模块,包括监听端口、监听域名等
- location {} URI 匹配
- 通过 include 可以将其他的配置文件包含进来
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
/etc/nginx/conf.d/default.conf
- 这个文件就默认配置了一个虚拟主机,通过 include 引入了上面的配置文件
- location 的作用就是进行 url 的匹配设置相应的参数,url 的写法支持正则表达式
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
二、反向代理
1、关于反向代理和负载均衡的简介
正向代理
- 假设我们需要使用自己的笔记本A访问国外的站点B,由于网络限制,我们无法访问国外的站点.但是此时我们有一台服务器C跳出了网络限制可以访问国外站点,那么我们可以通过正向代理技术,让我们笔记本访问服务器C,通过服务器C间接的访问国外站点B.这个过程就称之为正向代理
- 正向代理的一个特点:我们明确的知道我们要访问的是哪个站点
反向代理
- 假设一个站点访问量很大,单台的服务器A无法承受这种访问量,常规的做法就是多部署几台服务器做集群
- 对于服务器集群,用户并不知道该访问哪台服务器,所以这里必须增加一台反向代理服务器Nginx,用户访问Nginx,Nginx帮助我们将请求转发到任意一台服务器
负载均衡
- 负载均衡其实就是在Nginx服务器在转发用户请求之前,去判断服务器集群中的哪台服务器压力比较小,然后将用户的请求分配那台服务器压力小的服务器上,这种均匀分配的方式极大的减小了单台服务器的压力,避免了服务器崩溃的情况
参考文章:https://www.cnblogs.com/GreenLeaves/p/9901622.html
2、示例
最简单的反向代理
# https
server {
server_name www.example.com;
listen 443;
location /newchart/hollow/small/nsh000001.gif {
proxy_pass http://image.sinajs.cn/newchart/hollow/small/nsh000001.gif;
}
location /newchart/hollow/small/nsz399001.gif {
proxy_pass http://image.sinajs.cn/newchart/hollow/small/nsz399001.gif;
}