Nginx是一款轻量级的网页服务器、反向代理服务器。相较于Apache、lighttpd具有占有内存少,稳定性高等优势。它最常的用途是提供反向代理。
要注意的是,对于php项目来说,nginx和apache并不完全兼容,比如通过 $_SERVER获取某些请求内容时;详见:https://blog.csdn.net/robin_sky/article/details/80760367
## 添加nginx yum资源库
sudo rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
## 安装nginx
sudo yum install -y nginx
## 创建Nginx日志目录
mkdir /var/log/nginx/
## 设置开机启动
sudo systemctl enable nginx
## 启动服务
sudo systemctl start nginx
## 停止服务
sudo systemctl restart nginx
## 重载,重新配置后,不希望重启服务,就可以使用重新加载
sudo systemctl reload nginx
## 其它命令
## 验证配置的正确性
sudo nginx -t
## 启动
sudo /usr/sbin/nginx
## 重载
sudo nginx -s reload
nginx有两个常用的配置文件 default.conf
和 nginx.conf
,这两个配置文件本质上是一个; 在 default.conf
中引入了 nginx.conf
,他们只是按模块进行了划分,以方便管理
注意:nginx默认会转发所有请求头,但不包括带下划线的头,详见:https://juejin.im/post/6844904018725044231
cd /etc/nginx;
vim nginx.conf;
配置说明
#运行nginx的用户,默认即是nginx,可以不进行设置
user nginx;
#Nginx进程数量,一般设置为和CPU核数一样
worker_processes 1;
#错误日志存放目录
error_log /var/log/nginx/error.log warn;
#进程pid存放位置
pid /var/run/nginx.pid;
events {
worker_connections 1024; #单个后台进程的最大并发数
}
# http模块用于配置虚拟主机
http {
include /etc/nginx/mime.types; # 文件扩展名与类型映射表
default_type application/octet-stream; # 默认文件类型
# 设置日志模式,格式名称为main
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; #开启gzip压缩
#包含的子配置项位置和文件,这些文件里包含了各个虚拟主机的具体配置
include /etc/nginx/conf.d/*.conf;
}
为了保持高性能,ngixn对服务名称(域名)的存储桶和服务名称数量都有一定的限制;
1>. 服务名称server_name的哈希表存储桶大小限制
ngixn对server_name属性值的长度有限制,一般最长不能超过32或64个英文字符,其默认长度限制取决于CPU的缓存行长度;该限制可以自行设置,但必须是32的倍数;为了保持高性能,此值应该保存默认,详见官网描述;
http {
## 设置server_name属性值的长度有限制
server_names_hash_bucket_size 64;
.....
}
2>. 服务名称数量的限制
为了减小内存开销,nginx默认对服务名称数量有一定的限制,默认为512;此值应设为接近实际需要的服务名称数量的数字;此值也可自定义:
http {
## 设置server_name的最大数量限制
server_names_hash_max_size 512;
.....
}
为了减小内存开销,此值应该尽量的小,详见官网描述;
## 默认的子配置文件目录,此目录中的所有.conf配置文件都在nginx.conf中被包含
cd /etc/nginx/conf.d
## 此目录中默认有一个default.conf配置文件,可以自己创建更多配置文件
vim default.conf
default.conf服务配置
## 虚拟主机1或服务1
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; # 配置404页面
# redirect server error pages to the static page /50x.html
#
# 错误状态码的显示页面,配置后需要重启;即,500、502等这些错误码对应的错误页面
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;
#}
}
## 虚拟主机2或服务2
server {
# ...
}
配置详解:https://www.runoob.com/w3cnote/nginx-setup-intro.html
默认情况下,被Nginx反向代理的服务(如:Apache、Tomcat等)若在90内没有响应,则Nginx会报超时错误;
http {
# .....
# 0.1> 配置Nignx超时时间
# 后端服务器连接的超时时间
proxy_connect_timeout 86400; #单位秒
# 连接成功后,等待后端服务器响应的时间
proxy_send_timeout 86400; #单位秒
# 后端服务器数据回传时间,就是在规定时间之内后端服务器必须传完所有的数据
proxy_read_timeout 86400; #单位秒
# 0.2> 缓冲区大小
proxy_buffer_size 16k;
proxy_buffers 4 64k;
proxy_busy_buffers_size 128k;
proxy_temp_file_write_size 128k;
#包含的子配置项位置和文件,这些文件里包含了各个虚拟主机的具体配置
include /etc/nginx/conf.d/*.conf;
}
重启Nignx
sudo systemctl restart nginx