概述
百科诉说:Nginx (engine x) 是一个高性能的HTTP和反向代理web服务器,同时也提供了IMAP/POP3/SMTP服务。
Nginx是由伊戈尔·赛索耶夫为俄罗斯访问量第二的Rambler.ru站点(俄文:Рамблер)开发的,
第一个公开版本0.1.0发布于2004年10月4日。
Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,在BSD-like 协议下发行。
其特点是占有内存少,并发能力强,事实上nginx的并发能力在同类型的网页服务器中表现较好,中国大陆使用nginx网站用户有:百度、京东、新浪、网易、腾讯、淘宝等。
- 官方网址及下载地址:http://nginx.org/en/download.html
-
一、在Linux中安装nginx
1 环境要求
Centos 7
nginx-1.16.1.tar.gz
2 先安装nginx依赖的包
nginx是C语言开发,建议在linux上运行,所以我们选择在linux上安装Nginx
相关依赖包:
gcc
安装nginx需要先将官网下载的源码进行编译,编译依赖gcc环境,如果没有gcc环境,需要安装gcc
yum install gcc-c++
PCRE
PCRE(Perl Compatible Regular Expressions)是一个Perl库,包括 perl 兼容的正则表达式库。nginx的http模块使用pcre来解析正则表达式,所以需要在linux上安装pcre库。
yum install -y pcre pcre-devel
注:pcre-devel是使用pcre开发的一个二次开发库。nginx也需要此库。
zlib
zlib库提供了很多种压缩和解压缩的方式,nginx使用zlib对http包的内容进行gzip,所以需要在linux上安装zlib库。
yum install -y zlib zlib-devel
openssl
OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及SSL协议,并提供丰富的应用程序供测试或其它目的使用。
nginx不仅支持http协议,还支持https(即在ssl协议上传输http),所以需要在linux安装openssl库。
yum install -y openssl openssl-devel
3 把nginx的源码上传到linux系统,将nginx-1.16.1.tar.gz包拷贝到“/usr/local/src”下。然后解压。
4.安装
1、创建usr/nginx目录 ---安装目录 mkdir -p /usr/nginx
2、在解压的目录文件里,进入到/usr/local/src/nginx中运行configure执行文件(./configure --prefix=/usr/ngin)
3、编译安装即可:make install
4、检测安装:ls /usr/nginx
出现一下文件说明成功了:
Conf 配置文件
html 静态文件
logs 日志文件
sbin 命令 启动 停止等等命令
5、Nginx的启动、停止
启动
Cd /usr/nginx
Cd sbin
./nginx
停止
./nginx -s stop
刷新:当修改了conf文件夹里面配置文件之后要执行的命令
./nginx -s reload
nginx的配置文件说明:
nginx实现反向代理和负载均衡的配置和动静分离
注意:(可直接使用,只需要修改域名和server IP,如果使用动静分离需要设置创建相应的文件目录,并将项目中的静态资源复制到nginx创建的文件目录中。删除项目中的静态资源)
// An highlighted block
#user nobody;
#解决动静分离的权限访问
user root;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include 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 logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
#配置负载均衡 weight:权重
upstream www.erp.com{
ip_hash; #一种IP的算法,只要用户使用同一个IP请求,nginx会分流到之前请求的服务器
#least_conn; #以最少的活动连接数将请求发送到服务器,同样要考虑服务器权重。
server 192.168.43.186:8080 weight=1 max_fails=3 fail_timeout=3;
server 192.168.43.186:8090 weight=2 max_fails=3 fail_timeout=3;
server 192.168.43.186:8100 weight=3 max_fails=3 fail_timeout=3;
}
server {
#监听的端口
listen 80;
#映射域名
server_name www.erp.com;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
#root html;
#index index.html index.htm;
proxy_pass http://www.erp.com; #指向上面配置的server的IP节点
proxy_redirect off ;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 50m; #允许客户端请求的最大单文件字节数
client_body_buffer_size 256k;#缓冲区代理缓冲用户端请求的最大字节数,
proxy_connect_timeout 1; #nginx跟后端服务器连接超时时间(代理连接超时)
proxy_send_timeout 30; #后端服务器数据回传时间(代理发送超时)
proxy_read_timeout 60; #连接成功后,后端服务器响应时间(代理接收超时)
proxy_buffer_size 256k;#设置代理服务器(nginx)保存用户头信息的缓冲区大小
proxy_buffers 4 256k; #proxy_buffers缓冲区,网页平均在256k下,这样设置
proxy_busy_buffers_size 256k; #高负荷下缓冲大小(proxy_buffers*2)
proxy_temp_file_write_size 256k; #设定缓存文件夹大小
proxy_next_upstream error timeout invalid_header http_500 http_503 http_404;
proxy_max_temp_file_size 128m;
}
#动态分离的配置静态文件
location ~* \.(css|js|json|woff2*|woff*|ttf*)$ {
root resources/static; #可以使用相对路径和绝对路径 /usr/resources就是绝对路径
expires 7d; #有效天数
}
#动态分离的配置静态音视频文件
location ~* \.(avi|txt|png|gif|jpg|mp3|mp4|rmvb)$ {
root resources/static; #nginx根目录下的resources文件夹
expires 20d;
}
#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 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;
#}
}
# 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;
# }
#}
}