0
点赞
收藏
分享

微信扫一扫

web服务之Nginx网站服务

朱悟能_9ad4 2022-05-23 阅读 58

一、关于Nginx

一款高性能、轻量级Web服务软件
稳定性高
系统资源消耗低
对HTTP并发连接的处理能力高
单台物理服务器可支持30000~50000个并发请求

二、编译安装Nginx服务

1、关闭防火墙将nginx所需软件包到/opt目录下

```html/xml
systemctl stop firewalld
systemctl disable firewalld
setenforce 0


![image.png](https://s2.51cto.com/images/20220523/1653288031469713.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=)
#### 2、安装依赖包
```html/xml
#nginx的配置及运行需要pcre、zlib等软件包的支持,因此需要安装这些安装的开发包,以便提供相应的库和头文件。
yum -y install pcre-devel zlib-devel gcc gcc-c++ make

image.png

3、创建运行用户、组

Nginx 服务程序默认以 nobody 身份运行,建议为其创建专门的用户账号,以便更准确地控制其访问权限

useradd -M -s /sbin/nologin nginx

image.png

4、编译安装Nginx

cd /opt
tar zxvf nginx-1.12.0.tar.gz -C /opt/

cd /opt/nginx-1.12.0/

./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module

#-----配置命令解释
--prefix=/usr/local/nginx      #指定nginx的安装路径
--user=nginx                   #指定用户名
--group=nginx                  #指定组名
--with-http_stub_status_module #启用 http_stub_status_module 模块以支持状态统计
#-----

make && make install

#让系统识别nginx的操作命令
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/

image.png
image.png
image.png
image.png
image.png

5、检查、启动、重启、停止 nginx服务

#检查配置文件是否配置正确
nginx -t

#启动
nginx
#查看端口是否启动
netstat -natp | grep 80

#停止;先查看nginx的PID号
cat /usr/local/nginx/logs/nginx.pid
kill -3 
kill -s QUIT 
killall -3 nginx
killall -s QUIT nginx

#重启
kill -1 
kill -s HUP 
killall -1 nginx
killall -s HUP nginx

#日志分隔,重新打开日志文件
kill -USR1 

#平滑升级
kill -USR2 

image.png

6、添加 Nginx 系统服务

方法一:以脚本的方式管理服务,这里就不演示这个了,有兴趣可以自己试试

vim /etc/init.d/nginx
#!/bin/bash
#chkconfig: - 99 20
#description:Nginx Service Control Script
COM="/usr/local/nginx/sbin/nginx"
PID="/usr/local/nginx/logs/nginx.pid"
case "$1" in
start)
  $COM
;;

stop)
  kill -s QUIT $(cat $PID)
;;

restart)
  $0 stop
  $0 start
;;

reload)
  kill -s HUP $(cat $PID)
;;

*)
echo "Usage: $0 {start|stop|restart|reload}"
exit 1

esac
exit 0

#为脚本文件添加执行权限
chmod +x /etc/init.d/nginx
#添加为系统服务
chkconfig --add nginx
#重启服务
systemctl stop nginx
systemctl start nginx
#systemctl restart 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

image.png
image.png

三、认识Nginx服务的主配置文件 nginx.conf

vim /usr/local/nginx/conf/nginx.conf

1)全局配置

#2-9行左右
#运行用户,若编译时未指定则默认为 nobody
#user nobody;
#工作进程数量,可配置成服务器内核数 * 2
worker_processes 1;
#错误日志文件的位置
#error_log logs/error.log;
#PID 文件的位置
#pid logs/nginx.pid;

image.png

2)I/O 事件配置

#12-15行左右
events {
    #使用 epoll 模型,2.6及以上版本的系统内核,建议使用epoll模型以提高性能
    use epoll;
    #每个进程处理 4096 个连接
    worker_connections 4096;
}
#如提高每个进程的连接数还需执行“ulimit -n 65535”命令临时修改本地每个进程可以同时打开的最大文件数。
#在Linux平台上,在进行高并发TCP连接处理时,最高的并发数量都要受到系统对用户单一进程同时可打开文件数量的限制(这是因为系统为每个TCP连接都要创建一个socket句柄,每个socket句柄同时也是一个文件句柄)。
#可使用ulimit -a命令查看系统允许当前用户进程打开的文件数限制.

uname -r 查看内核版本

image.png
image.png

3)HTTP 配置

#18-57行左右
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.lisi.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;
    }
}
}

image.png

2、验证访问网页

systemctl restart nginx.service
echo "192.168.74.74 www.lisi.com" >> /etc/hosts
#使用浏览器访问
http://www.lisi.com/
http://192.168.74.74/

测试
image.png
image.png

3、日志格式设定

$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.lisi.com/test,会返回文件/usr/local/nginx/html/test/index.html
alias(别名配置)
请求www.lisi.com/test,会返回文件/usr/local/nginx/html/index.html

proxypass (反向代理配置)
#转发请求到http://127.0.0.1:8080/1.jpg
proxy_pass http://127.0.0.1:8080/;
#转发请求到http://127.0.0.1:8080/test/1.jpg
proxy_pass http://127.0.0.1:8080;

root和alias的区别
image.png
image.png
测试
image.png
image.png
image.png
image.png
四、访问状态统计配置

1、先查看Nginx服务中是否包含 HTTP_STUB_STATUS 模块

/usr/local/nginx/sbin/nginx -V

image.png

2、修改 nginx.conf 配置文件,指定访问位置并添加 stub_status 配置

cd /usr/local/nginx/conf
cp nginx.conf nginx.conf.bak
vim /usr/local/nginx/conf/nginx.conf
......
http {
......
  server {
    listen 80;
    server_name www.lisi.com;
    charset utf-8;
    location / {
      root html;
      index index.html index.php;
    }
    #添加stub_status配置
    location /status {
      stub_status on;
      access_log off;
    }
  }
}

image.png
image.png

3、重启服务,访问测试

systemctl restart nginx

浏览器访问 http://192.168.74.74/status
Active connections :表示当前的活动连接数;
server accepts handled requests :表示已经处理的连接信息,三个数字依次表示已处理的连接数、成功的TCP握手次数、 已处理的请求数。

image.png

五、基于授权的访问控制

1、生成用户密码认证文件

yum install -y httpd-tools
htpasswd -c /usr/local/nginx/passwd.db lisi
chown nginx /usr/local/nginx/passwd.db
chmod 400 /usr/local/nginx/passwd.db

image.png
image.png
image.png

2、修改主配置文件相对应目录,添加认证配置项

vim /usr/local/nginx/conf/nginx.conf
......
    server {
        location / {
         ......
          #添加认证配置
          auth_basic "secret";
          auth_basic_user_file /usr/local/nginx/passwd.db;
        }
    }

image.png

3、重启服务,访问测试

nginx -t
systemctl restart nginx

浏览器访问 http://192.168.74.74或www.lisi.com

image.png

六、基于客户端的访问控制

访问控制规则如下:

deny IP/IP 段:拒绝某个 IP 或 IP 段的客户端访问。
allow IP/IP 段:允许某个 IP 或 IP 段的客户端访问。
规则从上往下执行,如匹配则停止,不再往下匹配。

vim /usr/local/nginx/conf/nginx.conf
......
  server {
    location / {
    ......
    #添加控制规则
    #拒绝访问的客户端 IP
    deny 192.168.184.31;
    #允许其它IP客户端访问
    allow all;
    }
  }

systemctl restart nginx

七、基于域名的 Nginx 虚拟主机

1、为虚拟主机提供域名解析

echo "192.168.74.74 www.test1.com www.test2.com" >> /etc/hosts

image.png

2、为虚拟主机准备网页文档

mkdir -p /var/www/html/test1
mkdir -p /var/www/html/test2
echo "
www.test1.com
" > /var/www/html/test1/index.html
echo "
www.test2.com
" > /var/www/html/test2/index.html

image.png

3、修改Nginx的配置文件

vim /usr/local/nginx/conf/nginx.conf
......
http {
......
    server {
        listen 80;
        server_name  www.test1.com;
        charset utf-8;
        access_log logs/www.test1.access.log;

        location / {
            root   /var/www/html/test1;
            index  index.html index.php;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

     server {
         listen 80;
         server_name www.test2.com;
         charset utf-8;
         access_log logs/www.test2.access.log;

         location / {
              root /var/www/html/test2;
              index index.html index.php;
         }

         error_page 500 502 503 504 /50x.html;
               location = 50x.html{
               root html;
               }
         }
}

image.png

4、重启服务,访问测试

#检查语法
nginx -t
systemctl restart nginx

#浏览器访问
http://www.test1.com/
http://www.test2.com/

image.png
image.png

八、基于IP 的 Nginx 虚拟主机

1、增加网卡,添加域名解析

ifconfig ens33:0 192.168.74.100 netmask 255.255.255.0

echo "192.168.74.74 www.test1.com" >> /etc/hosts
echo "192.168.74.100 www.test2.com" >> /etc/hosts

2、修改Nginx的配置文件

vim /usr/local/nginx/conf/nginx.conf
......
http {
......
  server {
    listen 192.168.74.74:80;
    server_name  www.test1.com;
    charset utf-8;
    access_log logs/www.test1.access.log;
    location / {
      root   /var/www/html/test1;
      index  index.html index.php;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
      root   html;
    }
  }
  server {
    listen 192.168.74.100:80;
    server_name www.test2.com;
    charset utf-8;
    access_log logs/www.test2.access.log;
    location / {
      root /var/www/html/test2;
      index index.html index.php;
    }
    error_page 500 502 503 504 /50x.html;
    location = 50x.html{
      root html;
    }
  }
}

image.png

3、重启服务,访问测试

systemctl restart nginx
#浏览器访问
http://192.168.74.74/
http://192.168.74.100/

image.png
image.png

九、基于端口的 Nginx 虚拟主机

```html/xml
vim /usr/local/nginx/conf/nginx.conf
......
http {
......
server {
listen 192.168.74.74:80;
server_name www.test1.com;
charset utf-8;
access_log logs/www.test1.access.log;
location / {
root /var/www/html/test1;
index index.html index.php;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 192.168.74.100:8080;
server_name www.test2.com;
charset utf-8;
access_log logs/www.test2.access.log;
location / {
root /var/www/html/test2;
index index.html index.php;
}
error_page 500 502 503 504 /50x.html;
location = 50x.html{
root html;
}
}
}

systemctl restart nginx
浏览器访问
http://192.168.74.74:80
http://192.168.74.74:8080


![image.png](https://s2.51cto.com/images/20220523/1653312108324055.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=)
![image.png](https://s2.51cto.com/images/20220523/1653312503463049.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=)
![image.png](https://s2.51cto.com/images/20220523/1653312545835916.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=)
举报

相关推荐

Nginx网站服务

Nginx 网站服务

nginx网站服务

Web网站服务

nginx WEB服务

Nginx网站服务配置

0 条评论