下载nginx
- 下载“nginx-1.20.0.tar.gz”,移动到/usr/local/src下。
- 解压
tar -zxvf nginx-1.20.0.tar.gz - 进入nginx目录
cd nginx-1.20.0 - 简单配置
./configure --prefix=/usr/local/nginx
make && make install
# 如果make出现该错误
# make: *** No rule to make target `build', needed by `default'. Stop.,
# 主要是因为没有先决条件,执行`yum -y install make zlib-devel gcc-c++ libtool openssl openssl-devel`
# 然后重新 ./configure --prefix=/usr/local/nginx
# make && make install
请注意以上方式只是基本的安装 此时并没有ssl模块
重新安装模块
- 添加模块
- --with-http_stub_status_module: 性能统计模块
- --with-http_ssl_module: ssl模块
- --with-http_v2_module: http2模块
- --with-http_gzip_static_module: 扩展压缩模块
- --with-http_realip_module: 获取真实ip
- --with-stream:nginx从1.9.0版本开始,新增了ngx_stream_core_module模块,使nginx支持四层负载均衡。默认编译的时候该模块并未编译进去,需要编译的时候添加--with-stream,使其支持stream代理
- nginx-static-etags/ngx_cache_purge: 缓存模块
cd /usr/local/nginx/conf/
git clone git://github.com/mikewest/nginx-static-etags.git ./nginx-static-etags
git clone https://github.com/FRiCKLE/ngx_cache_purge.git ./ngx_cache_purge-2.1
cd /usr/local/src/nginx-1.20.0/
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module --with-http_gzip_static_module --with-http_realip_module --with-stream
make
注意:
- 如果出现:
# nginx error: the HTTP rewrite module requires the PCRE library.
安装pcre-devel:
yum -y install pcre-devel解决问题
- 如果错误是:
./configure: error: the HTTP cache module requires md5 functions from OpenSSL library. You can either disable the module by using --without-http-cache option, or install the OpenSSL library into the system, or build the OpenSSL library statically from the source with nginx by using --with-http_ssl_module --with-openssl=<path> options.
执行
yum -y install openssl openssl-devel解决问题
- 回到源码目录,objs文件夹中多了一个nginx的文件,这个就是新版本的程序了。首先我们把之前的nginx先备份一下,然后把新的程序复制过去覆盖之前的即可
cd /usr/local/src/nginx-1.20.0/
cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak
cp ./objs/nginx /usr/local/nginx/sbin/nginx
- 最后我们来到Nginx安装目录下,来查看模块是否安装成功
cd /usr/local/nginx/sbin
./nginx -V
返回值configure arguments为:
configure arguments: --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module --with-http_gzip_static_module --with-http_realip_module --with-stream
常用命令
-
启动操作
/usr/nginx/sbin/nginx (/usr/nginx/sbin/nginx -t 查看配置信息是否正确) -
停止操作
- 查询nginx主进程号
ps -ef | grep nginx在进程列表里面找master进程,它的编号就是主进程号了
- 发送信号
从容停止:
kill -QUIT 主进程号
快速停止:kill -TERM 主进程号
强制停止:pkill -9 nginx
另外,若在nginx.conf配置了pid文件存放路径则该文件存放的就是Nginx主进程号,如果没指定则放在nginx的logs目录下。有了pid文件,我们可以不用查询Nginx的主进程号,而是直接向Nginx发送信号了,命令如下:kill -信号类型 '/usr/nginx/logs/nginx.pid'
- 平滑重启
平滑重启:
kill -HUP 主进程号或进程号文件路径
常用方法:/usr/local/nginx/sbin/nginx -s reload
注意,修改了配置文件后最好先检查一下修改过的配置文件是否正确,以免重启后Nginx出现错误影响服务器稳定运行。判断Nginx配置是否正确命令如下:
nginx -t -c /usr/nginx/conf/nginx.conf
或者
/usr/nginx/sbin/nginx -t
最终配置参考
upstream nodeApi {
server localhost:3002 weight=1 max_fails=2 fail_timeout=30s;
server localhost:3003 weight=2 max_fails=2 fail_timeout=30s;
ip_hash;
}
server {
listen 443 ssl http2;
server_name xxx.xxx.xxx;
charset urf-8;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
# gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
gzip_vary off;
gzip_disable "MSIE [1-6]\.";
ssl_certificate /data/ssl/xxx.xxx.xxx.crt;
ssl_certificate_key /data/ssl/xxx.xxx.xxx.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://localhost:9999;
}
location /status {
stub_status on;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|ico|rar|css|js|zip|xml|txt|flv|swf|mid|doc|cur|xls|pdf|txt|)$ {
access_log off;
expires 30d;
add_header Cache-Control max-age=2592000;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://localhost:9999;
}
location ~ index\.html$ {
access_log off;
# html文件因为名称不会改变,所以使用协商缓存,html文件有改动就会立即更新,max-age=no-cache代表进入协商缓存,文件改动会自动更新,不改动会返回304
# add_header Cache-Control max-age=no-cache;
etag on;
expires 30d
}
location ~ ^/api/(.*)$ {
proxy_redirect off;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_next_upstream http_502 http_504 error timeout invalid_header;
proxy_pass http://nodeApi/$1$is_args$args;
}
}
总结
本文章主要教大家安装ssl模块以及一些常用命令,如果您感觉对您有帮助,烦请您点个赞再走。 具体详细的配置您可以参考我的另一篇文章:nginx配置