1.什么是反向代理服务器:以代理服务器来接受internet上的连接请求,然后将请求转发给内部网络上的服务器,并将内部服务器上获取的结果返回给internet上请求连接的客户端。此时代理服务器对外就表现为一个反向代理服务器。
2.Nginx的主要作用是反向代理和负载均衡。
3.Nginx的安装过程:
3.1.安装编译包和依赖包
#yum install gcc gcc-c++ openssl openssl-devel zib-devel zib
3.2.创建运行nginx的用户
#groupadd www
#useradd -M -s /sbin/nologin -g www www --不允许www用户登录
3.3.安装pcre
下载pcre wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre2-10.21.tar.bz
ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/
解压:tar -jxvf pcre-8.38.tar.bz2
编译安装:#cd pcre-8.38
#./configure && make -j4 && make install
#ldconfig
3.4.安装nginx
下载:nginx:wget http://nginx.org/download/nginx-1.6.2.tar.gz
解压:#tar xf nginx-1.6.2.tar.gz
#cd nginx-1.6.2
#./configure --prefix=/usr/local/nginx(指定安装路径) --with-pcre --user=www --group=www --with-http_stub_status_module
#make -j4 && make install
3.5.编辑nginx配置文件:配置文件在nginx根目录的conf目录下的nginx.conf
#vi /usr/local/nginx/conf/nginx.conf
worker_processes 8 #设置跟cpu核数一样就行
worker_rlimit_nofile 309600;
events {
worker_connections 309600;
use epoll;
}
3.6.启动nginx
检查nginx配置文件语法有没问题
#/usr/local/nginx/sbin/nginx -t
注:lib缺少文件进一步查看具体内容:ldd $(which /usr/local/nginx/sbin/nginx)
注:创建软连接
对于/lib/* 32位系统来说:
#查看lib库
# ls /lib/ |grep pcre
libpcre.so.0
libpcre.so.0.0.1
#添加软连接
# ln -s /lib/libpcre.so.0.0.1 /lib/libpcre.so.1
对于/lib64/* 64位系统来说:
#查看lib库
# ls /lib64/ |grep pcre
libpcre.so.0
libpcre.so.0.0.1
#添加软连接
# ln -s /lib64/libpcre.so.0.0.1 /lib64/libpcre.so.1
------------------------------
/usr/sbin/groupadd -f www
/usr/sbin/useradd -g www www
-----------------------------
#/usr/local/nginx/sbin/nginx
重新加载nginx:/usr/local/nginx/sbin/nginx -s reload
3.7.启动843端口
下载843程序:wget http://demo.yunfancdn.com/flash843.zip
下载后解压:cp ./flash843.zip /usr/local/
unzip /usr/local/flash843.zip
赋予执行权限:
cd /usr/local/flash843/
chmod +x flash843 #843主程序
chmod +x check_task #843守护进程
4.Nginx配置文件介绍
4.1Nginx配置文件结构介绍
#全局块
events {#events块
}
http #http块
{
... #http全局块
server #server块
{
... #server全局块
location [PATTERN] #location块
{
...
}
location [PATTERN]
{
...
}
}
server
{
...
}
... #http全局块
}
4.2 Nginx配置文件说明
#user nobody; #配置用户或者组,默认为nobody nobody。
worker_processes 1;#设置和CPU核数一样,允许生成的进程数,默认为1
#制定日志路径,级别。这个设置可以放入全局块,http块,server块,级别以此为:debug|info|notice|warn|error|crit|alert|emerg
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid; #指定nginx进程运行文件存放地址
events {
#accept_mutex on; #设置网路连接序列化,防止惊群现象发生,默认为on
#multi_accept on; #设置一个进程是否同时接受多个网络连接,默认为off
#use epoll; #事件驱动模型,select|poll|kqueue|epoll|resig|/dev/poll|eventport
worker_connections 1024;#最大的并发数
}
http {
include mime.types; #文件扩展名与文件类型映射表
default_type application/octet-stream; #默认文件类型,默认为text/plain
#access_log off; #取消服务日志
#自定义格式
#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 logs/access.log main;
sendfile on; #允许sendfile方式传输文件,默认为off,可以在http块,server块,location块。
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65; #连接超时时间,默认为75s,可以在http,server,location块。
#gzip on;
#设定负载均衡的服务器列表
upstream mysvr{
server 23.83.239.221:8080 weight=5;
server 23.83.239.221:8090 weight=1;
#server 192.168.10.121:3333 backup; #热备
}
server {
#keepalive_requests 120; #单连接请求上限次数。
listen 80;#监听端口
server_name mysvr;#主机名,监听的地址
#charset koi8-r;
#access_log logs/host.access.log main;
#定义默认请求
location / {
proxy_pass http://mysvr;
root html;
index index.html index.htm;
}
#location ~*^.+$ { #请求的url过滤,正则匹配,~为区分大小写,~*为不区分大小写。
# #root path; #根目录
# #index vv.txt; #设置默认页
# proxy_pass http://mysvr; #请求转向mysvr 定义的服务器列表
# deny 127.0.0.1; #拒绝的ip
# allow 172.18.5.54; #允许的ip
# }
#定义错误提示页面
#error_page 404 /404.html;
#error_page 404 https://www.baidu.com; #错误页
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
proxy_pass http://mysvr;
root 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; #拒绝的ip
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}