目录
一款高性能、轻量级Web服务软件
NG并发连接能力受以下二个因素的影响:
CPU个数
本地物理服务器系统的最大文件打开数
首先关闭防火墙
- systemctl stop firewalld
- systemctl disable firewalld
- setenforce 0
-
-
将安装包上传到/opt目录下
安装依赖环境包
yum -y install pcre-devel zlib-devel gcc gcc-c++ make
创建用户组
useradd -M -s /sbin/nologin nginx
解压软件包,然后配置软件的模块
- tar zxf nginx-1.15.9.tar.gz
- cd nginx-1.15.9/
- ./configure \
- > --prefix=/usr/local/nginx \
- > --user=nginx \
- > --group=nginx \
- > --with-http_stub_status_module
-
编译安装
make && make install
优化配置文件路径,便于系统识别nginx的操作命令
- ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
- nginx -t ##检查配置文件是否配置正确

添加nginx系统服务
- vim /lib/systemd/system/nginx.service
- [Unit]
- Description=nginx
- After=network.target
- [Service]
- Type=forking
- PIDFile=/usr/local/nginx/logs/nginx.pid
- ExecStart=/usr/local/nginx/sbin/nginx
- ExecrReload=/bin/kill -s HUP $MAINPID
- ExecrStop=/bin/kill -s QUIT $MAINPID
- PrivateTmp=true
- [Install]
- WantedBy=multi-user.target
-

赋权,重启服务并且设置开机自启
- chmod 754 /lib/systemd/system/nginx.service
- systemctl start nginx.service
- systemctl enable nginx.service

验证服务

(nginx.conf路径:/usr/local/nginx/conf/nginx.conf)
全局配置
- #user nobody; #运行用户,若编译时未指定则默认为 nobody
- worker_processes 1; #工作进程数量,可配置成服务器内核数 * 2
- #error_log logs/error.log; #错误日志文件的位置
- #pid logs/nginx.pid; #PID 文件的位置
I/O事件配置
- events {
- use epoll; #使用 epoll 模型,2.6及以上版本的系统内核,建议使用epoll模型以提高性能
- worker_connections 4096; #每个进程处理 4096 个连接
- }
- #如提高每个进程的连接数还需执行“ulimit -n 65535”命令临时修改本地每个进程可以同时打开的最大文件数。
- #在Linux平台上,在进行高并发TCP连接处理时,最高的并发数量都要受到系统对用户单一进程同时可打开文件数量的限制(这是因为系统为每个TCP连接都要创建一个socket句柄,每个socket句柄同时也是一个文件句柄)。
- #可使用ulimit -a命令查看系统允许当前用户进程打开的文件数限制.
ulimt -a可以查看每个进程可处理的文件数量
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;
- ##此选项允许或禁止使用socke的TCP_CORK的选项(发送数据包前先缓存数据),此选项仅在使用sendfile的时候使用
- #tcp_nopush on;
-
- ##连接保持超时时间,单位是秒
- #keepalive_timeout 0;
- keepalive_timeout 65;
-
- ##gzip模块设置,设置是否开启gzip压缩输出
- #gzip on;
-
- ##Web 服务的监听配置
- server {
- ##监听地址及端口
- listen 80;
- ##站点域名,可以有多个,用空格隔开
- server_name www.lxx.com;
-
- ##网页的默认字符集
- charset utf-8;
-
- ##根目录配置
- location / {
-
- ##网站根目录的位置/usr/local/nginx/html
- root html;
-
- ##默认首页文件名
- index index.html index.htm;
- }
- ##内部错误的反馈页面
- error_page 500 502 503 504 /50x.html;
- ##错误页面配置
- location = /50x.html {
- root html;
- }
- }
- }
日志格式设定
- $remote_addr与$http_x_forwarded_for用以记录客户端的ip地址;
- $remote_user:用来记录客户端用户名称;
- $time_local: 用来记录访问时间与时区;
- $request: 用来记录请求的url与http协议;
- $status: 用来记录请求状态;成功是200,
- $body_bytes_sent :记录发送给客户端文件主体内容大小;
- $http_referer:用来记录从那个页面链接访问过来的;
- $http_user_agent:记录客户浏览器的相关信息;
-
- 通常web服务器放在反向代理的后面,这样就不能获取到客户的IP地址了,通过$remote_add拿到的IP地址是反向代理服务器的iP地址。反向代理服务器在转发请求的http头信息中,可以增加x_forwarded_for信息,用以记录原有客户端的IP地址和原来客户端的请求的服务器地址。
- location常见配置指令,root、alias、proxy_pass
-
- root(根路径配置):请求www.lic.com/test,会返回文件/usr/local/nginx/html/test/index.html
-
- alias(别名配置):请求www.lic.com/test,会返回文件/usr/local/nginx/html/index.html
基于客户端的访问控制
1、访问控制规则如下
deny IP/IP段:拒绝某个IP或IP段的客户端访问
allow IP/IP段:允许某个IP或IP段的客户端的访问
规则从上往下执行,如匹配则停止,不再往下匹配
- vim /usr/local/nginx/conf/nginx.conf
- location / {
- root html;
- index index.html index.htm;
- deny 192.168.200.112; #添加拒绝访问的客户端的IP
- allow all; #添加允许其他IP客户端访问
- }
添加域名解析
- echo "192.168.200.112 www.lxx.com www.faiz.com" >> /etc/hosts
- mkdir -p /var/www/html/lxx
- mkdir -p /var/www/html/faiz
- echo "
www.lxx.com
" >/var/www/html/lxx/index.html - echo "
www.faiz.com
" >/var/www/html/faiz/index.html
vim /usr/local/nginx/conf/nginx.conf

测试


- vim /usr/local/nginx/conf/nginx.conf
-
- server {
- listen 192.168.200.111:80;
- server_name www.faiz.com;
- charset utf-8;
- access_log logs/faiz.access.log;
- location / {
- root /var/www/html/faiz;
- index index.html index.htm;
- }
- error_page 500 502 503 504 /50x.html;
- location = /50x.html {
- root html;
- }
- }
-
- server {
- listen 192.168.200.111:8080;
- server_name www.joker.com;
- charset utf-8;
- access_log logs/joker.access.log;
- location / {
- root /var/www/html/joker;
- index index.html index.htm;
- }
- error_page 500 502 503 504 /50x.html;
- location = /50x.html {
- root html;
- }
-
- }
测试


vim /etc/hosts


修改配置文件
- vim /usr/local/nginx/conf/nginx.conf
-
-
- server {
- listen 192.168.200.111;
- server_name www.faiz.com;
- charset utf-8;
- access_log logs/faiz.access.log;
- location / {
- root /var/www/html/faiz;
- index index.html index.htm;
- }
- error_page 500 502 503 504 /50x.html;
- location = /50x.html {
- root html;
- }
- }
-
- server {
- listen 192.168.200.122;
- server_name www.joker.com;
- charset utf-8;
- access_log logs/joker.access.log;
- location / {
- root /var/www/html/joker;
- index index.html index.htm;
- }
- error_page 500 502 503 504 /50x.html;
- location = /50x.html {
- root html;
- }
-
- }
-
测试

