Linux 部署 Nginx 服务

Nginx介绍

Nginx 是一款轻量级高性能的web服务器和反向代理服务器

Nginx的安装

  1. 通过yum安装
  1. 在保证网络正常,可以访问互联网的情况下,配置yum源
  2. 使用 yum 安装 nginx

使用 yum 命令安装的nginx服务启动方式及常用命令:
systemctl start nginx 启动nginx服务
systemctl stop nginx 停止nginx服务
systemctl restart nginx 重启nginx服务
systemctl reload nginx 重载nginx配置
nginx -t 检查nginx服务配置是否正确

具体yum命令可参考 中的【Linux 系统配置命令-软件安装命令】

[root@centos7 ~]# curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
[root@centos7 ~]# curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
[root@centos7 ~]# yum install nginx
# 使用rpm -ql nginx 查询nginx的按照文件位置
[root@centos ~]# rpm -ql nginx
/etc/logrotate.d/nginx
/etc/nginx/fastcgi.conf
/etc/nginx/fastcgi.conf.default
/etc/nginx/fastcgi_params
/etc/nginx/fastcgi_params.default
/etc/nginx/koi-utf
/etc/nginx/koi-win
/etc/nginx/mime.types
/etc/nginx/mime.types.default
/etc/nginx/nginx.conf
/etc/nginx/nginx.conf.default
/etc/nginx/scgi_params
/etc/nginx/scgi_params.default
/etc/nginx/uwsgi_params
/etc/nginx/uwsgi_params.default
/etc/nginx/win-utf
/usr/bin/nginx-upgrade
/usr/lib/systemd/system/nginx.service
/usr/lib64/nginx/modules
/usr/sbin/nginx
/usr/share/doc/nginx-1.16.1
/usr/share/doc/nginx-1.16.1/CHANGES
/usr/share/doc/nginx-1.16.1/README
/usr/share/doc/nginx-1.16.1/README.dynamic
/usr/share/doc/nginx-1.16.1/UPGRADE-NOTES-1.6-to-1.10
/usr/share/licenses/nginx-1.16.1
/usr/share/licenses/nginx-1.16.1/LICENSE
/usr/share/man/man3/nginx.3pm.gz
/usr/share/man/man8/nginx-upgrade.8.gz
/usr/share/man/man8/nginx.8.gz
/usr/share/nginx/html/404.html
/usr/share/nginx/html/50x.html
/usr/share/nginx/html/en-US
/usr/share/nginx/html/icons
/usr/share/nginx/html/icons/poweredby.png
/usr/share/nginx/html/img
/usr/share/nginx/html/index.html
/usr/share/nginx/html/nginx-logo.png
/usr/share/nginx/html/poweredby.png
/usr/share/vim/vimfiles/ftdetect/nginx.vim
/usr/share/vim/vimfiles/ftplugin/nginx.vim
/usr/share/vim/vimfiles/indent/nginx.vim
/usr/share/vim/vimfiles/syntax/nginx.vim
/var/lib/nginx
/var/lib/nginx/tmp
/var/log/nginx

安装文件解释:

  1. /etc/nginx 目录下一是一些配置文件
  2. /usr/share/nginx 目录下是一些静态资源文件
  3. /var/log/nginx 下是一些日志文件及临时文件
  1. 通过make安装nginx
  1. 下载nginx安装包到Linux
  2. 创建配置nginx需要的用户(作为运行nginx的用户,为了安全,不允许该用户登录系统)
  3. 解压压缩包并进入nginx目录
  4. 由于使用的是编译安装,所有需要自己解决软件的依赖,nginx软件所需依赖:yum install -y pcre-devel openssl-devel
  5. 执行配置操作
  6. make编译及安装
wget http://nginx.org/download/nginx-1.18.0.tar.gz
[root@centos7 ~]# ll
总用量 1016
-rw-r--r-- 1 root root 1039530 4月  21 22:33 nginx-1.18.0.tar.gz
[root@centos7 ~/nginx-1.18.0]# useradd -M -s /sbin/nologin www
[root@centos7 ~/nginx-1.18.0]# id www
uid=1002(www) gid=1002(www)=1002(www)
[root@centos7 ~]# tar xvf nginx-1.18.0.tar.gz
[root@centos7 ~]# ll
总用量 1016
drwxr-xr-x 8 test 1001     158 4月  21 22:09 nginx-1.18.0
-rw-r--r-- 1 root root 1039530 4月  21 22:33 nginx-1.18.0.tar.gz
[root@centos7 ~]# cd nginx-1.18.0/
[root@centos7 ~/nginx-1.18.0]# ll
总用量 760
drwxr-xr-x 6 test 1001    326 9月  29 15:38 auto
-rw-r--r-- 1 test 1001 302863 4月  21 22:09 CHANGES
-rw-r--r-- 1 test 1001 462213 4月  21 22:09 CHANGES.ru
drwxr-xr-x 2 test 1001    168 9月  29 15:38 conf
-rwxr-xr-x 1 test 1001   2502 4月  21 22:09 configure
drwxr-xr-x 4 test 1001     72 9月  29 15:38 contrib
drwxr-xr-x 2 test 1001     40 9月  29 15:38 html
-rw-r--r-- 1 test 1001   1397 4月  21 22:09 LICENSE
drwxr-xr-x 2 test 1001     21 9月  29 15:38 man
-rw-r--r-- 1 test 1001     49 4月  21 22:09 README
drwxr-xr-x 9 test 1001     91 9月  29 15:38 src
[root@centos7 ~/nginx-1.18.0]# yum install -y pcre-devel openssl-devel
[root@centos7 ~/nginx-1.18.0]# ./configure --prefix=/opt/nginx --user=www --with-http_ssl_module --with-http_stub_status_module
[root@centos7 ~/nginx-1.18.0]# make && make install
[root@centos7 ~]# cd /opt/nginx/
[root@centos7 /opt/nginx]# ll
总用量 0
drwx------ 2 www  root   6 9月  29 15:54 client_body_temp
drwxr-xr-x 2 root root 333 9月  29 16:21 conf
drwx------ 2 www  root   6 9月  29 15:54 fastcgi_temp
drwxr-xr-x 2 root root  40 9月  29 15:53 html
drwxr-xr-x 2 root root  41 9月  29 16:31 logs
drwx------ 2 www  root   6 9月  29 15:54 proxy_temp
drwxr-xr-x 2 root root  19 9月  29 15:53 sbin
drwx------ 2 www  root   6 9月  29 15:54 scgi_temp
drwx------ 2 www  root   6 9月  29 15:54 uwsgi_temp

./configure --prefix=/opt/nginx --user=www --with-http_ssl_module --with-http_stub_status_module
配置项解释:

  1. - -prefix=/opt/nginx 指定 nginx 的安装目录,若目录不存在,会自动创建
  2. - -user=www 指定运行 nginx worker 进程的用户
  3. - -with-http_ssl_module 开可使用 ssl - https 访问功能
  4. - -with-http_stub_status_module 开启 nginx 服务监控功能(必选)

nginx 安装文件解释:
/opt/nginx/log 存放日志的目录
/opt/nginx/html 存放 html 代码静态文件的位置
/opt/nginx/conf 存放配置文件的位置
/opt/nginx/sbin nginx 可执行文件位置,可以进入该目录使用./nginx 来启动服务

为了可以在系统中任意位置执行命令开启 nginx,将 nginx 目录添加到环境变量中:

[root@centos7 ~]# export PATH=$PATH:/opt/nginx/sbin
[root@centos7 ~]# nginx -t
nginx: the configuration file /opt/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /opt/nginx/conf/nginx.conf test is successful

若想永久生效,需要把 export PATH=$PATH:/opt/nginx/sbin 追加写入到~/.bashrc 中;
若需对所有用户永久生效,需要写入到/etc/bashrc中;
然后执行 source ~/.bashrc 或 source /etc/bashrc 立即生效

Nginx的配置

这里以 yum 安装的 nginx 为例

  • /etc/nginx/nginx.conf 配置文件:
[root@centos ~]# cd /etc/nginx/
[root@centos /etc/nginx]# ll
total 68
drwxr-xr-x 2 root root   73 Sep 29 15:18 conf.d
drwxr-xr-x 2 root root    6 Oct  3  2019 default.d
-rw-r--r-- 1 root root 1077 Oct  3  2019 fastcgi.conf
-rw-r--r-- 1 root root 1077 Oct  3  2019 fastcgi.conf.default
-rw-r--r-- 1 root root 1007 Oct  3  2019 fastcgi_params
-rw-r--r-- 1 root root 1007 Oct  3  2019 fastcgi_params.default
-rw-r--r-- 1 root root 2837 Oct  3  2019 koi-utf
-rw-r--r-- 1 root root 2223 Oct  3  2019 koi-win
-rw-r--r-- 1 root root 5231 Oct  3  2019 mime.types
-rw-r--r-- 1 root root 5231 Oct  3  2019 mime.types.default
-rw-r--r-- 1 root root 2491 Sep 28 09:40 nginx.conf
-rw-r--r-- 1 root root 2656 Oct  3  2019 nginx.conf.default
-rw-r--r-- 1 root root  636 Oct  3  2019 scgi_params
-rw-r--r-- 1 root root  636 Oct  3  2019 scgi_params.default
-rw-r--r-- 1 root root  664 Oct  3  2019 uwsgi_params
-rw-r--r-- 1 root root  664 Oct  3  2019 uwsgi_params.default
-rw-r--r-- 1 root root 3610 Oct  3  2019 win-utf

先来看 /etc/nginx/nginx.conf 文件:

[root@centos /etc/nginx]# cat nginx.conf
# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

# worker进程用户
user nginx;    
# worker 进程数量,要根据CPU来配置
worker_processes auto;
# 错误日志
error_log /var/log/nginx/error.log;
# 进程 pid 位置----有时服务启动有问题可以检测一些该文件
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
# 模块配置文件
include /usr/share/nginx/modules/*.conf;


events {
	# 设置每个worker进程最大连接数
    worker_connections 1024;
}

http {
	# 日志格式
    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  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    # 长连接,保持连接超时时间
    keepalive_timeout   65;
    types_hash_max_size 2048;
	
	# 导入处理文件类型配置文件
    include             /etc/nginx/mime.types;
    # 默认处理文件方式(若在以上/etc/nginx/mime.types文件中没有匹配到)
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    # 导入/etc/nginx/conf.d/下所有的配置文件,可在/etc/nginx/conf.d/目录中为不同站点创建配置不同策略的配置文件
    include /etc/nginx/conf.d/*.conf;
	
	# 站点配置
    server {
    	# IPv4 侦听端口,客户端通过该端口访问nginx服务;default_server默认加载的站点,全局只能有一个,主要配置
        listen       80 default_server;
        # IPv6 侦听端口
        listen       [::]:80 default_server;
        # 服务名,多个不同站点,通过该配置进行路由
        server_name  _;
        # 站点根目录
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        # 导入默认配置
        include /etc/nginx/default.d/*.conf;
		
		# 页面配置项,可为不同页面配置不同的项
        location / {
        }
		
		# 404错误页
        error_page 404 /404.html;
            location = /40x.html {
        }
		
		# 错误页
        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }

# https 配置
# Settings for a TLS enabled server.
#
#    server {
#        listen       443 ssl http2 default_server;
#        listen       [::]:443 ssl http2 default_server;
#        server_name  _;
#        root         /usr/share/nginx/html;
#
#        ssl_certificate "/etc/pki/nginx/server.crt";
#        ssl_certificate_key "/etc/pki/nginx/private/server.key";
#        ssl_session_cache shared:SSL:1m;
#        ssl_session_timeout  10m;
#        ssl_ciphers HIGH:!aNULL:!MD5;
#        ssl_prefer_server_ciphers on;
#
#        # Load configuration files for the default server block.
#        include /etc/nginx/default.d/*.conf;
#
#        location / {
#        }
#
#        error_page 404 /404.html;
#            location = /40x.html {
#        }
#
#        error_page 500 502 503 504 /50x.html;
#            location = /50x.html {
#        }
#    }

}
  1. 全局块:配置影响nginx全局的指令。一般有运行nginx服务器的用户组,nginx进程pid存放路径,日志存放路径,配置文件引入,允许生成worker process数等。
  2. events块:配置影响nginx服务器或与用户的网络连接。如每个进程的最大连接数,选取哪种事件驱动模型处理连接请求,是否允许同时接受多个网路连接,开启多个网络连接序列化等。
  3. http块:可以嵌套多个server,配置代理,缓存,日志定义等绝大多数功能和第三方模块的配置。如文件引入,mime-type定义,日志自定义,是否使用sendfile传输文件,连接超时时间,单连接请求数等。
  4. server块:配置虚拟主机的相关参数,一个http中可以有多个server。
  5. location块:配置请求的路由,以及各种页面的处理情况。
  • /etc/nginx/conf.d/* 配置文件

写在前面
在做以下配置实验时,建议做以下操作

  1. systemctl stop firewalld 关闭firewalld 防火墙服务,该项会影响浏览器访问网站
  2. setenforce 0 关闭SELinux,该项会影响Nginx访问文件,提供静态资源
  3. 修改配置后,若要配置生效,需要重启服务或者重载配置
    nginx -s reloadsystemctl restart nginx
  1. 基本静态网站配置
[root@centos /etc/nginx/conf.d]# pwd
/etc/nginx/conf.d
# 在该目录中创建指定文件,文件名随意,但是最好是可以标识站点的名称
[root@centos /etc/nginx/conf.d]# cat www.conf 
# server块,配置该站点
server {
	# 侦听80端口,取消默认,否则和/etc/nginx/nginx.conf文件冲突,或者可以将/etc/nginx/nginx.conf文件中的server块配置注释
    listen       80;
    # 服务名称,也就是站点名成,当客户端输入域名访问时,nginx会对用户访问的站点进行路由,根据该配置查找对应的站点
    server_name  www.master.com;
    # 站点的根目录
    root         /var/html/www;
    # 设置首页文件名称,当用户不指定目录或文件,直接访问域名时,返回该页面
    index        index.html;
}

[root@centos /var/html/www]# pwd
/var/html/www
[root@centos /var/html/www]# ll
total 4
-rw-r--r-- 1 root root 16 Sep 29 19:11 index.html
[root@centos /var/html/www]# cat index.html 
<h1>Master</h1>

验证:

  1. 若浏览器所在主机为Windows,则需要修改C:\Windows\System32\drivers\etc\hosts 文件,在文件末尾添加10.0.0.201          www.master.com,前边是Linux虚拟机的IP地址,后边是域名;Windows该文件为DNS解析文件,在访问域名是,系统优先查找该文件进行DNS解析,即将域名解析为可路由的IP地址。
  2. 若浏览器所在主机为Linux,则需要修改/etc/hosts/,在文件末尾添加10.0.0.201          www.master.com。
  3. 在配置好本地DNS解析文件后即可使用浏览器访问对应的域名。

在这里插入图片描述
2. 文件共享网站配置

  • 免密版:
[root@centos /etc/nginx/conf.d]# pwd 
/etc/nginx/conf.d
[root@centos /etc/nginx/conf.d]# cat share.conf 
server {
    listen       80;
    server_name  share.master.com;
    root         /var/html/share/;
    autoindex    on;    # 站点目录文件列表索引,实现文件共享
}

[root@centos /var/html/share]# pwd
/var/html/share
[root@centos /var/html/share]# ll
total 0
drwxr-xr-x 2 root root 23 Sep 29 19:38 test
-rw-r--r-- 1 root root  0 Sep 29 19:38 test.txt

如图为访问网站截图:

在这里插入图片描述

  • 访问地址白名单配置

访问地址白名单,即配置只有某些IP地址或者IP地址段的客户端可以访问网站或网站某页面

[root@centos /etc/nginx/conf.d]# pwd
/etc/nginx/conf.d
[root@centos /etc/nginx/conf.d]# cat share.conf 
server {
    listen       80;
    server_name  share.master.com;
    root         /var/html/share/;
    autoindex    on;
    allow        10.0.0.2;    # 允许访问的客户端地址
    deny         all;         # 拒绝访问的客户端地址,all表示全部
}

客户端地址为10.0.0.1,访问被拒绝
在这里插入图片描述

使用location块来配置只允许10.0.0.2的地址访问test目录

[root@centos /etc/nginx/conf.d]# cat share.conf 
server {
    listen       80;
    server_name  share.master.com;
    root         /var/html/share/;
    autoindex    on;
    location /test/ {     # 配置/test/ 目录,只有10.0.0.2才可以访问
        allow        10.0.0.2;
        deny         all;
    }
}

访问首页可以,但是进入test目录时被拒绝
在这里插入图片描述

  • 登录验证版

若要登录验证,需要用到用户名和密码,这个需要一个工具htpasswd来向文件中写入用户名及密码
yum install httpd-tools 安装工具包

[root@centos /etc/nginx]# pwd
/etc/nginx
# htpasswd -c 参数创建passwd.txt 文件并添加test用户
[root@centos /etc/nginx]# htpasswd -c passwd.txt test
New password: 
Re-type new password: 
Adding password for user test
[root@centos /etc/nginx]# ll passwd.txt 
-rw-r--r-- 1 root root 43 Sep 29 20:09 passwd.txt
[root@centos /etc/nginx]# cat passwd.txt 
test:$apr1$wE5Q1Bxc$EMe5kvH4b6nG7bAi2xqbq.
# 再次向passwd.txt 中添加用户
[root@centos /etc/nginx]# htpasswd passwd.txt user
New password: 
Re-type new password: 
Adding password for user user
[root@centos /etc/nginx]# cat passwd.txt 
test:$apr1$wE5Q1Bxc$EMe5kvH4b6nG7bAi2xqbq.
user:$apr1$Tte5F1Gb$pkakogJe38e94f5xwrohS/

安装好htpasswd工具并创建用户密码文件后,需要修改配置文件,开启用户身份验证功能,并指定用户密码文件

# 本例只做限制某目录访问验证
[root@centos /etc/nginx/conf.d]# pwd
/etc/nginx/conf.d
[root@centos /etc/nginx/conf.d]# cat share.conf 
server {
    listen       80;
    server_name  share.master.com;
    root         /var/html/share/;
    autoindex    on;
    location /test/ {
        auth_basic    "test dir auth login";    # 验证说明,是用来做test目录登录验证的
        auth_basic_user_file   /etc/nginx/passwd.txt;   # 用户密码文件位置
    }
}

效果如图所示:
在这里插入图片描述
在这里插入图片描述

  • 网站服务状态监控
  1. 将网站状态监控放在sever块上
[root@centos /etc/nginx/conf.d]# pwd 
/etc/nginx/conf.d
[root@centos /etc/nginx/conf.d]# cat www.conf 
server {
    listen       80;
    server_name  www.master.com;
    root         /var/html/www;
    index        index.html;
    stub_status; 
}
[root@centos /var/html/www]# tree
.
├── a
│   └── a.html
└── index.html

1 directory, 2 files

可以看到,当访问其他目录中的页面时,页面显示也是网站状态信息
在这里插入图片描述
2. 对指定目录配置网站状态监控

[root@centos /etc/nginx/conf.d]# pwd
/etc/nginx/conf.d
[root@centos /etc/nginx/conf.d]# cat www.conf 
server {
    listen       80;
    server_name  www.master.com;
    root         /var/html/www;
    index        index.html;
    location /status/ {
        stub_status; 
    }
}
# 根据以上配置,需要载www.master.com的站点目录下创建status目录
[root@centos /var/html/www]# pwd
/var/html/www
[root@centos /var/html/www]# mkdir status
[root@centos /var/html/www]# ll
total 4
drwxr-xr-x 2 root root 20 Sep 29 20:25 a
-rw-r--r-- 1 root root 16 Sep 29 19:11 index.html
drwxr-xr-x 2 root root  6 Sep 29 20:32 status

访问站点如下图,当访问status目录时显示网站状态信息,其他页面访问正常
在这里插入图片描述

网站状态信息说明
# 网站活动的连接数,可以通过 netstat -pantu | grep nginx 查看 ESTABLISHED状态的连接数
Active connections: 2
accepts:建立连接的总数
handled:处理连接的总数
requests:请求包总的数量
server accepts handled requests
               47           47         96
Reading: 0 Writing: 1 Waiting: 1

Nginx的日志

yum 安装的 nginx 日志文件位置为 /etc/log/nginx

  1. access.log 通过的访问日志
  2. error.log 错误日志
[root@centos /var/log/nginx]# cat access.log
10.0.0.1 - test [29/Sep/2020:20:40:12 +0800] "GET /test/ HTTP/1.1" 200 271 "http://share.master.com/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36 Edg/85.0.564.63" "-"
   ①        ②               ③                           ④           ⑤               ⑥                             ⑦

该日志格式对应 /etc/nginx/nginx.conf 中配置的日志格式
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 /var/log/nginx/access.log main;
① 客户端访问IP地址
② 若为登录认证版,该项为登录用户名,否则为 -
③ 请求时间
④ 请求头
⑤ 响应状态
⑥ Referer 客户是从哪个页面跳转过来的访问该页面的
⑦ User-Agent 可标识用户浏览器,及操作系统

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值