一、前提条件:openssl、pcre已经安装
1、查看openssl是否安装
openssl version -a
2、查看是否安装pcre,安装会显示版本, 没安装什么都不显示
rpm -qa pcre
二、Nginx安装
1、下载nginx安装包
[root@localhost nginx]# wget http://nginx.org/download/nginx-1.21.1.tar.gz
2、安装部署
1)解压
[root@localhost nginx]# tar zxvf nginx-1.21.1.tar.gz
2)编译
[root@localhost nginx-1.21.1]# ./configure --prefix=/home/httpd/nginx1211 --with-http_stub_status_module --with-http_ssl_module --with-pcre
3)安装
[root@localhost nginx-1.21.1]#make
[root@localhost nginx-1.21.1]#make install
或者
[root@localhost nginx-1.21.1]#make && make install
3、查看安装成功后版本信息
[root@localhost nginx]# ./usr/local/nginx/sbin/nginx -v
nginx version: nginx/1.21.1
四、配置文件
1、配置nginx.conf文件(默认端口为80)
vi /usr/local/nginx/conf/nginx.conf
#Nginx用户及组:用户 组。window下不指定 ,以实际为准
#user nobody;
user ruready ruready;
#工作进程:数目。根据硬件调整,通常等于CPU数量或者2倍于CPU。
worker_processes 2;
#错误日志:存放路径。
error_log /usr/local/nginx/logs/error.log crit; # 日志位置和日志级别
#pid(进程标识符):存放路径
pid /usr/local/nginx/logs/nginx.pid;
#一个进程能打开的文件描述符最大值,理论上该值因该是最多能打开的文件数除以进程数。
#但是由于nginx负载并不是完全均衡的,所以这个值最好等于最多能打开的文件数。
#LINUX系统可以执行 sysctl -a | grep fs.file 可以看到linux文件描述符。
worker_rlimit_nofile 65535;
events {
#使用epoll的I/O 模型。linux建议epoll,FreeBSD建议采用kqueue,window下不指定。
use epoll;
#单个进程最大连接数(最大连接数=连接数*进程数)
worker_connections 65535;
#客户端请求头部的缓冲区大小。这个可以根据你的系统分页大小来设置,
#一般一个请求头的大小不会超过1k,不过由于一般系统分页都要大于1k,所以这里设置为分页大小。
#client_header_buffer_size 4k;
}
http {
#设定mime类型,类型由mime.type文件定义
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"';
#用了log_format指令设置了日志格式之后,需要用access_log指令指定日志文件的存放路径
#记录了哪些用户,哪些页面以及用户浏览器、ip和其他的访问信息
access_log /usr/local/nginx/logs/access.log main;
#开启文件传输,一般应用都应设置为on;若是有下载的应用,则可以设置成off来平衡网络I/O和磁盘的I/O来降低系统负载
sendfile on;
#告诉nginx在一个数据包里发送所有头文件,而不一个接一个的发送。
#tcp_nodelay off 会增加通信的延时,但是会提高带宽利用率。在高延时、数据量大的通信场景中应该会有不错的效果
#tcp_nodelay on,会增加小包的数量,但是可以提高响应速度。在及时性高的通信场景中应该会有不错的效果
tcp_nopush on;
#长连接超时时间,单位是秒
keepalive_timeout 60;
#gzip模块设置,使用 gzip 压缩可以降低网站带宽消耗,同时提升访问速度。
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
#负载均衡
#max_fails为允许请求失败的次数,默认为1
#weight为轮询权重,根据不同的权重分配可以用来平衡服务器的访问率。
# upstream myServer{
# server 192.168.247.129:8080 max_fails=3 weight=2;
# server 192.168.247.129:8081 max_fails=3 weight=4;
# }
#管理后台端(兼容HTTPS)
server {
listen 80;#监听端口
#IP/域名可以有多个,用空格隔开
server_name www.test.com;#域名
charset utf-8;
access_log /usr/local/nginx/logs/host.access.log main;
location / {
}
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;
# }
#}
}
2、检查配置文件的正确性
/usr/local/nginx/sbin/nginx -t
[root@localhost ~]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
五、Nginx服务启动
1、启动
/usr/local/nginx/sbin/nginx
[root@localhost conf]# /usr/local/nginx/sbin/nginx
[root@localhost conf]# ps -ef | grep nginx
root 57398 1 0 19:57 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
ruready 57399 57398 13 19:57 ? 00:00:01 nginx: worker process
ruready 57400 57398 8 19:57 ? 00:00:00 nginx: worker process
root 57410 40384 0 19:57 pts/2 00:00:00 grep --color=auto nginx
2、停止
nginx -s stop
[root@localhost sbin]# ./nginx -s stop
root 50768 1 0 17:04 ? 00:00:00 nginx: master process ./nginx
nobody 51096 50768 0 17:18 ? 00:00:00 nginx: worker process
root 51153 50509 0 17:21 pts/0 00:00:00 grep --color=auto nginx
查看服务是否停止
[root@localhost sbin]# ps -ef|grep nginx
root 51204 50509 0 17:25 pts/0 00:00:00 grep --color=auto nginx
3、配置文件修改后重新加载Nginx服务
进入nginx可执行目录sbin下,输入命令./nginx -s reload 即可
[root@localhost sbin]# ./nginx -s reload
4、访问测试
配置的端口是8001,所以输入http://localhost:8001,即可访问。
六、Nginx版本信息隐藏
编辑Nginx的配置文件 nginx.conf,在 http{ … } 块中添加参数 server_tokens off; 即可;
修改后需要重启Nginx才会生效;当需要显示版本信息时,只需将 server_tokens 设为 on
[root@localhost conf]# vi nginx.conf
[root@localhost conf]# ./../sbin/nginx -s reload