安装
环境
- 服务器:test11 :192.168.37.11
- 系统版本:Centos 7.6
- 软件版本:nginx 1.17
- 官网地址:http://nginx.org/
采用yum安装
[root@test11 ~]# yum install yum-utils
[root@test11 ~]# vim /etc/yum.repos.d/nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
[root@test11 ~]# yum-config-manager --enable nginx-mainline
[root@test11 ~]# yum install nginx -y
[root@test11 ~]# nginx -v
nginx version: nginx/1.17.2
安装完毕后,可以用nginx -V
查看yum安装nginx的默认参数
采用编译安装
安装依赖包:
yum -y install gcc make pcre pcre-devel openssl openssl-devel zlib zlib-devel
下载源码包并解压
wget http://nginx.org/download/nginx-1.17.2.tar.gz tar -zxvf nginx-1.17.2.tar.gz
创建用户:
groupadd www & useradd -g www -s /sbin/nologin www
编译安装,指定安装位置和要安装的模块
cd nginx-1.17.2 ./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_ssl_module --with-http_spdy_module --with-http_stub_status_module --with-pcre make && make install
验证:
/usr/local/nginx/sbin/nginx -V
编译安装完成后再添加模块
下载要添加的模块,并放到指定的目录下,我们在/usr/local/nginx下新建一个modules目录专门放模块的
查询当前nginx编译模块:
nginx -V
到原来安装的nginx版本目录下,将原来的编译参数填写上,并用–add-module是添加新模块,指定模块文件夹位置,例如我们添加一个nginx_upstream_check_module模块,如下:
./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_ssl_module --with-http_spdy_module --with-http_stub_status_module --with-pcre --add-module=/usr/local/nginx/modules/nginx_upstream_check_module
编译,不要install,不然覆盖了,注意看状态,
echo $?
为0就是正常的:make
替换,make后将在当前nginx源码文件夹下有个objs文件夹,里面有个nginx文件,把它替换原来的就可以了
cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak cp objs/nginx /usr/local/nginx/sbin/
验证,用
nginx -V
查看参数是不是有新添加的模块了
yum安装完成后再添加模块
请参考:与LDAP集合认证的例子
配置
- yum安装完的nginx默认文件位置是/etc/nginx/nginx.conf,这个是nginx的主配置文件,我们通常在上面配置nginx的日志格式,优化参数,所包含的子配置目录位置等。
- 一般来说,为了使配置更易于维护,建议在/etc/nginx/nginx.d/下创建相应的目录,并把一些特定功能的文件存储在新创建的目录下,并用include在主nginx.conf中引用,如下:
http{ #HTTP流量
.
.
server{
.
.
}
include /etc/nginx/nginx.d/vhost/*.conf; #新建的虚拟服务器配置文件都放在这个目录下
}
stream{ #TCP和UDP流量
include /etc/nginx/nginx.d/stream/*.conf;
}
- 配置完nginx后可用
nginx -t
来查看nginx配置文件有没有问题,没有问题的用nginx -s reload
重新加载nginx
以下是nginx在线上服务器做反向代理的配置文件,可以参考一下:
user www;
worker_processes auto;
error_log logs/error.log error;
pid logs/nginx.pid;
worker_rlimit_nofile 1048576;
events { #events指令是设定nginx的工作模式及连接数上限
use epoll; #这是高性能高并发的工作模式
worker_connections 131072; #于定义nginx每个进程的最大连接数,默认是1024
}
http
{
include mime.types;
default_type application/octet-stream;
server_names_hash_max_size 2048;
client_max_body_size 50m; #设置最大的允许客户端主体大小,如果超过此配置值,客户端会收到413错误
client_body_buffer_size 64k;
sendfile on; #开启文件的高效传输模式
server_tokens off; #隐藏nginx版本号
gzip on; #开启gzip压缩功能
gzip_min_length 1k; #设置允许压缩页面最小字节数。建议设置成1k,如果小于1k可能会越压越大
gzip_comp_level 6;
#这里表示压缩级别,可以是0到9中的任一个,级别越高,压缩就越小,节省了带宽资源,但同时也消耗CPU资源,官网建议为6
gzip_types text/plain application/x-javascript text/css application/xml;
#这里表示哪些类型的文件要压缩,text/html类型是默认的不需要写。
gzip_vary on; #表示在传送数据时,给客户端说明我使用了gzip压缩
proxy_next_upstream error timeout http_500 http_502;
log_not_found off;
# SSL
ssl_ciphers "";
ssl_prefer_server_ciphers on;
ssl_session_cache shared:SSL:50m;
ssl_session_timeout 10m;
log_format main '$remote_addr - $remote_user [$time_local] '
'"$request" $status $body_bytes_sent '
'"$http_referer" "$http_user_agent" '
'$http_x_forwarded_for $upstream_addr $request_time $upstream_response_time $scheme';
server
{
listen 80;
server_name localhost;
location / {
return 403;
}
location /status {
stub_status on;
}
access_log logs/access.log main;
}
include vhost/*.conf;
include upstream/*.conf;
}
stream
{
include stream/*.conf;
}
文档更新时间: 2020-05-27 11:27 作者:子木