Nginx
下载
http://nginx.org/en/download.html
安装
Windows
解压即可
Linux
在线安装:
# 安装nginx
yum install -y nginx
# 启动nginx
systemctl start nginx
# 停止nginx
systemctl stop nginx
# 重启nginx
systemctl restart nginx
# 重载nginx
systemctl reload nginx
# 查看nginx服务端口
netstat -anpl | grep 'nginx'
yum方式安装的默认地址和配置的默认地址
# yum方式安装后默认配置文件的路径
/etc/nginx/nginx.conf
# nginx网站默认存放目录
/usr/share/nginx/html
# 网站默认主页路径
/usr/share/nginx/html/index.html
如果提示 没有可用软件包 nginx
添加CentOS 7 Nginx yum资源库
rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
离线安装
在安装nginx前首先要确认系统中安装了gcc、pcre-devel、zlib-devel、openssl-devel。
例如gcc:
yum list installed | grep "gcc"
安装命令:
yum -y install gcc gcc-c++ pcre-devel zlib-devel openssl openssl-devel
安装Nginx
##进入nginx目录
cd nginx-1.9.9
## 配置
./configure --prefix=/usr/local/nginx --with-http_ssl_module
# make
make && make install
Nginx常用命令常用命令
# 查看版本:
nginx -v
# 查看nginx安装目录:
ps -ef | grep nginx
# 检查配置文件:
nginx -t
# 启动:
systemctl start nginx.service
# 停止:
systemctl stop nginx.service
nginx -s stop
nginx -s quit
#重启:
systemctl restart nginx.service
# 设置开机自启动:
systemctl enable nginx.service
# 停止开机自启动:
systemctl disable nginx.service
# 查看当前状态:
systemctl status nginx.service
# 查看所有已启动的服务:
systemctl list-units --type=service
常用配置
# 配置允许运行nginx服务器的用户和用户组
#user nobody;
## 配置允许nginx进程生成的worker process数量,一般与CPU核数相等
worker_processes 4;
## worker进程最大打开文件数,解决"too many open files"问题
#worker_rlimit_nofile 100000
## 运行错误日志存放路径
error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
## pid文件存放路径和名称
pid logs/nginx.pid;
events {
## 事件驱动模型,select、poll、epoll等
use epoll;
## 最大连接数,直接决定并发处理能力
worker_connections 1024;
}
## http配置
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压缩,超过1024Kb才压缩
gzip on;
gzip_min_length 1024;
server {
listen 8000;
server_name localhost;
location / {
root /mnt;
}
}
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root 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 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;
#}
}
## 在这里导入子配置文件!!!
include /usr/local/nginx/conf/conf.d/*.conf;
}
https://www.digitalocean.com/community/tools/nginx?global.app.lang=zhCN
结构
- src目录:存放nginx的源代码
- docs目录:存放nginx的帮助文档
- html目录:默认网站文件
- contrib目录:存放其他机构或组织贡献的文档资料
- conf目录:存放nginx服务器的配置文件
- auto目录:存放大量脚本文件,和configure脚本程序相关
- configure目录:Nginx自动安装脚本,用于检查环境,生成编译代码的makefile相关文件
负载均衡
一般轮询负载均衡
upstream web_server {
server 192.168.124.1;
server 192.168.124.2;
}
server {
listen 80;
server_name location;
location / {
proxy_pass http://web_server;
}
}
加权负载均衡
upstream web_server {
server 192.168.124.1 weight=1;
server 192.168.124.2 weight=3;
}
server {
listen 80;
server_name location;
location / {
proxy_pass http://web_server;
}
}
常用状态参数
配置方式 | 说明 |
---|---|
max_fails | 允许请求失败的次数,默认为1。当超过最大次数时,返回proxy_next_upstream指令定义的错误 |
fail_timeout | 在经历了max_fails次失败后,暂停服务的时间。且在实际应用中max_fails一般与fail _timeout一起使用 |
backup | 预留的备份机器 |
down | 表示当前的 server暂时不参与负载均衡 |
upstream web_server {
server 192.168.124.1 weight=1 max_fails=1 fail_timeout=2;
server 192.168.124.2 weight=3 max_fails=2 fail_timeout=2;
server 192.168.124.3 backup;
}
server {
listen 80;
server_name location;
location / {
proxy_pass http://web_server;
}
}
设置为 backup 的服务器,只有当其他所有的非 backup 机器出现故障或者忙碌的情况下,才会请求 backup服务器,因此这台服务器的压力最小。
IP_HASH负载均衡
upstream web_server {
ip_hash;
server 192.168.124.1;
server 192.168.124.2;
server 192.168.124.3 down;
}
server {
listen 80;
server_name location;
location / {
proxy_pass http://web_server;
}
}
对于一个暂时性宕机的服务器,可以使用down参数标识出来,这样在负载均衡时,就会忽略该服务器的分配。
问题
root和alias的区别?
alias在映射路径时不会追加location匹配的部分,而root会追加location匹配到的部分