经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 数据库/运维 » Linux/Shell » 查看文章
Linux之Nginx入门
来源:cnblogs  作者:JZEason  时间:2022/1/4 0:15:47  对本文有异议

一、Nginx介绍

1.1 概述

Nginx是一个开源且高性能、可靠的http web服务、代理服务。

  • 开源:直接获取源代码

  • 高性能:支持海量并发

  • 可靠:服务稳定

1.2 Nginx特点

  • 高性能,高并发

    Nginx支持很高的并发,Nginx在处理大量并发的情况下比其他web服务要快

  • 轻量且高扩展性

    • 轻量:功能模块少,只保留核心模块,其他代码模块化 (易读,便于二次开发,对于开发人员非常友好)
    • 高扩展性:需要什么模块再安装模块,不需要全部安装,并且还支持第三方模块
  • 高可靠性

    只要不过分几乎不会出现问题,其他的web服务需要每隔一段时间进行重启,Nginx不需要;Nginx的宕机时间,是99999级别

  • 支持热部署

    Nginx可以在运行期间,更新迭代,代码部署。

  • 大多公司都在用Nginx

    • Nginx技术成熟,具备的功能是企业最常使用而且最需要的;
    • 适合当前主流架构趋势, 微服务、云架构、中间层;
    • 统一技术栈, 降低维护成本, 降低技术更新成本;
  • Nginx使用的是Epoll网络模型

    • Select: 当用户发起一次请求,select模型就会进行一次遍历扫描,从而导致性能低下。
    • Epoll: 当用户发起一次请求,epoll模型会直接进行处理,效率高效,并无连接限制。
  • Nginx应用场景

    img

1.3 其他web服务

  1. apache:httpd,最早期使用的web服务,性能不高,操作难
  2. nginx
    tengine:Tengine是由淘宝网发起的Web服务器项目。它在Nginx的基础上,针对大访问量网站的需求,添加了很多高级功能和特性
    openresty-nginx:OpenResty 是一个基于 Nginx 与 Lua 的高性能 Web 平台,其内部集成了大量精良的 Lua 库、第三方模块以及大多数的依赖项。用于方便地搭建能够处理超高并发、扩展性极高的动态 Web 应用、Web 服务和动态网关。
  3. IIS:windows下的web服务
  4. lighttpd:是一个德国人领导的开源 Web 服务器软件,其根本的目的是提供一个专门针对高性能网站,安全、快速、兼容性好并且灵活的 Web Server 环境。具有非常低的内存开销,CPU 占用率低,效能好,以及丰富的模块等特点。
  5. GWS:google web server
  6. BWS:baidu web server

Tomcat
Resin
weblogic
Jboss

……

二、Nginx安装

2.1 安装方式

  1. 1.epol源安装 web01
  2. 2.官方源安装 web03
  3. 3.源码包安装 web03
  4. #三种安装方式都需要安装nginx运行的依赖环境
  5. yum install -y gcc gcc-c++ autoconf pcre pcre-devel make automake wget httpd-tools vim tree

2.2 epol源安装

  1. [root@web01 ~]# yum install -y nginx

2.3 官方源安装

配置官方源:

  1. #配置官方源:
  2. [root@web02 ~]# vim /etc/yum.repos.d/nginx.repo
  3. [nginx-stable]
  4. name=nginx stable repo
  5. baseurl=http://nginx.org/packages/centos/7/$basearch/
  6. gpgcheck=1
  7. enabled=1
  8. gpgkey=https://nginx.org/keys/nginx_signing.key
  9. module_hotfixes=true
  10. #下载:
  11. [root@web02 ~]# yum install -y nginx

2.4 源码包安装

  1. 下载安装包

    1. [root@web03 ~]# wget https://nginx.org/download/nginx-1.20.2.tar.gz
  2. 解压源码包

    1. [root@web03 ~]# tar -xf nginx-1.20.2.tar.gz
  3. 配置安装的环境(运行用户,安装目录等)

    1. 1.创建用户和组,且不创建用户的家目录
    2. [root@web03 ~]# groupadd www -g 666
    3. [root@web03 ~]# useradd www -u 666 -g 666 -s /sbin/nologin -M
    4. 2.创建一个安装目录
    5. 公司不指定安装目录时,默认安装到/usr/local/软件名/
    6. 公司指定的话,就要按照公司要求来
  4. 生成Makefile

    1. [root@web03 ~/nginx-1.20.2]# cd nginx-1.20.2/
    2. [root@web03 ~/nginx-1.20.2]# ./configure --prefix=/usr/local/nginx-1.20.2 --user=www --group=www --without-http_gzip_module
  5. 编译安装,结束后去安装目录查看安装结果

    1. 1.编译安装
    2. [root@web03 nginx-1.20.2]# make && make install
    3. 2.查看安装结果
    4. [root@web03 /usr/local]# cd /usr/local/nginx-1.20.2/
    5. [root@web03 /usr/local/nginx-1.20.2]# ll
  6. 做软链接

    1. [root@web03 /usr/local/nginx-1.20.2]# ln -s /usr/local/nginx-nginx-1.20.2 /usr/local/nginx
  7. 配置环境变量

    1. [root@web03 ~]# vim /etc/profile.d/nginx.sh
    2. export PATH=$PATH:/usr/local/nginx/sbin
    3. ~
    4. [root@web03 ~]# source /etc/profile #在当前bash环境下读取并执行/etc/profile中的命令
  8. system管理配置

    1. #源码包安装后没有办法使用system管理,需要我们自己配置
    2. [root@web03 /usr/lib/systemd/system]# vim /usr/lib/systemd/system/nginx.service
    3. [Unit]
    4. Description=nginx - high performance web server
    5. Documentation=http://nginx.org/en/docs/
    6. After=network-online.target remote-fs.target nss-lookup.target
    7. Wants=network-online.target
    8. [Service]
    9. Type=forking
    10. PIDFile=/usr/local/nginx/logs/nginx.pid
    11. ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
    12. ExecReload=/usr/local/nginx/sbin/nginx -s reload
    13. ExecStop=/usr/local/nginx/sbin/nginx -s stop
    14. [Install]
    15. WantedBy=multi-user.target
    16. [root@web03 ~]# systemctl daemon-reload #加载新的unit (*.service)配置文件

三、Nginx命令

  1. nginx #启动nginx。 等价于systemctl start nginx
  2. nginx -s reopen #重启Nginx。 等价于systemctl restart nginx
  3. nginx -s reload #重新加载Nginx配置文件,然后以优雅的方式重启Nginx。 等价于systemctl reload nginx
  4. nginx -s stop #强制停止Nginx服务。 等价于systemctl stop nginx
  5. nginx -s quit #优雅地停止Nginx服务(即处理完所有请求后再停止服务)
  6. nginx -t #检测配置文件是否有语法错误,然后退出
  7. nginx -?,-h #打开帮助信息
  8. nginx -v #显示版本号信息并退出
  9. nginx -V #显示版本号和配置选项信息,然后退出
  10. nginx -V 2>&1 | sed "s/\s\+--/\n --/g" #模块分行输出,格式化输出
  11. killall nginx #杀死所有nginx进程
  12. systemctl enable nginx #加入开机自启动
  13. Centos6
  14. 启动:nginx
  15. service nginx start
  16. /etc/init.d/nginx start
  17. 加入开机自启:
  18. chkconfig nginx on
  19. nginx -T #检测配置文件是否有语法错误,转储并退出
  20. nginx -q #在检测配置文件期间屏蔽非错误信息(打印错误日志)
  21. nginx -p prefix #设置nginx的工作目录(默认是:/usr/share/nginx/)
  22. nginx -c filename #设置配置文件路径(默认是:/etc/nginx/nginx.conf)
  23. nginx -g directives #设置配置文件外的全局指令

四、Nginx服务平滑无感知添加模块和升级

4.1 模块解析

  1. [root@web02 ~]# nginx -V #显示版本和配置选项信息,然后退出
  2. nginx version: nginx/1.20.2
  3. built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
  4. built with OpenSSL 1.0.2k-fips 26 Jan 2017
  5. TLS SNI support enabled
  6. configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie
  7. ############################内容解析#######################################
  8. nginx version: nginx/1.20.2 #nginx版本
  9. built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
  10. built with OpenSSL 1.0.2k-fips 26 Jan 2017
  11. TLS SNI support enabled
  12. configure arguments:
  13. --prefix=/etc/nginx #nginx文件的安装路径,其它选项如果使用相对路径,那么以此路径为基础路径
  14. --sbin-path=/usr/sbin/nginx #二进制程序的安装目录
  15. --modules-path=/usr/lib64/nginx/modules #模块安装路径
  16. --conf-path=/etc/nginx/nginx.conf #设置nginx的conf文件路径
  17. --error-log-path=/var/log/nginx/error.log #设置nginx错误日志路径
  18. --http-log-path=/var/log/nginx/access.log #设置nginx的访问日志路径
  19. --pid-path=/var/run/nginx.pid #设置nginx的pid文件路径
  20. --lock-path=/var/run/nginx.lock #设置lock文件临时存放的路径
  21. --http-client-body-temp-path=/var/cache/nginx/client_temp #设置http客户端主体临时缓存路径
  22. --http-proxy-temp-path=/var/cache/nginx/proxy_temp #设置http反向代理临时路径
  23. --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp #设置http的fastcgi临时缓存路径
  24. --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp #设置uwsgi临时目录
  25. --http-scgi-temp-path=/var/cache/nginx/scgi_temp #scgi临时存放目录
  26. --user=nginx #设置启动 worker 进程时所使用的非特权用户名
  27. --group=nginx #设置启动 worker 进程时所使用的非特权用户组名
  28. --with-compat
  29. --with-file-aio
  30. --with-threads
  31. ......

4.2 Nginx服务添加模块

  1. 1.安装依赖
  2. [root@web02 nginx-1.20.2]# yum install -y openssl openssl-devel
  3. 2.再生成一次
  4. [root@web02 nginx-1.20.2]# ./configure --prefix=/usr/local/nginx-1.20.2-new --user=www --group=www --without-http_gzip_module --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'
  5. 3.安装
  6. [root@web02 nginx-1.20.2]# make && make install
  7. 4.重做软连接
  8. [root@web02 ~]# rm -rf /usr/local/nginx && ln -s /usr/local/nginx-1.20.2-new /usr/local/nginx
  9. 5.重启服务
  10. [root@web02 nginx-1.20.2]# systemctl restart nginx

4.3 Nginx升级

  1. 1.下载新版本的包
  2. [root@web02 ~]# wget http://nginx.org/download/nginx-1.20.2.tar.gz
  3. 2.解压
  4. [root@web02 ~]# tar xf nginx-1.20.2.tar.gz
  5. 3.生成
  6. [root@web02 nginx-1.20.2]# cd nginx-1.20.2
  7. [root@web02 nginx-1.20.2]# ./configure --prefix=/usr/local/nginx-1.20.2 --user=www --group=www --without-http_gzip_module --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module
  8. 4.编译安装
  9. [root@web02 nginx-1.20.2]# make && make install
  10. 5.重做软连接
  11. [root@web02 ~]# rm -rf /usr/local/nginx && ln -s /usr/local/nginx-1.20.2 /usr/local/nginx
  12. 6.重启服务
  13. [root@web02 ~]# systemctl restart nginx

五、Nginx相关文件

为了让大家更清晰的了解Nginx软件的全貌,可使用rpm -ql nginx查看整体的目录结构及对应的功能,如下表格整理了Nginx比较重要的配置文件。

5.1 Nginx主配置文件

路径 类型 作用
/etc/nginx/nginx.conf 配置文件 nginx主配置文件
/etc/nginx/conf.d/default.conf 配置文件 默认网站配置文件

5.2Nginx代理相关参数文件

路径 类型 作用
/etc/nginx/fastcgi_params 配置文件 Fastcgi代理配置文件
/etc/nginx/scgi_params 配置文件 scgi代理配置文件
/etc/nginx/uwsgi_params 配置文件 uwsgi代理配置文件

5.3 Nginx编码相关配置文件

路径 类型 作用
/etc/nginx/win-utf 配置文件 Nginx编码转换映射文件
/etc/nginx/koi-utf 配置文件 Nginx编码转换映射文件
/etc/nginx/koi-win 配置文件 Nginx编码转换映射文件
/etc/nginx/mime.types 配置文件 Content-Type与扩展名

5.4 Nginx管理相关命令

路径 类型 作用
/usr/sbin/nginx 命令 Nginx命令行管理终端工具
/usr/sbin/nginx-debug 命令 Nginx命令行与终端调试工具

5.5 Nginx日志相关目录与文件

路径 类型 作用
/var/log/nginx 目录 Nginx默认存放日志目录
/etc/logrotate.d/nginx 配置文件 Nginx默认的日志切割

六、Nginx配置文件

Nginx主配置文件/etc/nginx/nginx.conf是一个纯文本类型的文件,整个配置文件是以区块的形式组织的。一般,每个区块以一对大括号{}来表示开始与结束。

Nginx主配置文件整体分为三块进行学习,分别是CoreModule(核心模块),EventModule(事件驱动模块),HttpCoreModule(http内核模块)

配置文件内容:

  1. [root@web01 ~]# cat /etc/nginx/nginx.conf
  2. #########################核心模块####################
  3. #指定启动的用户
  4. user www;
  5. #nginx的worker进程的数量,auto==CPU数量
  6. worker_processes auto;
  7. #指定错误日志存放的路径以及记录的级别 debug/info/notice/warn/error/emerg
  8. error_log /var/log/nginx/error.log notice;
  9. #指定pid文件
  10. pid /var/run/nginx.pid;
  11. ########################事件驱动模块#################
  12. events {
  13. #每个worker工作进程的最大连接数
  14. worker_connections 1024;
  15. }
  16. ######################http内核模块###################
  17. http {
  18. #包含,nginx可识别的文件类型
  19. include /etc/nginx/mime.types;
  20. #当nginx不识别文件类型的时候,默认下载
  21. default_type application/octet-stream;
  22. #指定日志格式,日志格式起个名字
  23. log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  24. '$status $body_bytes_sent "$http_referer" '
  25. '"$http_user_agent" "$http_x_forwarded_for"';
  26. #指定访问日志存储路径与格式
  27. access_log /var/log/nginx/access.log main;
  28. #高效传输
  29. sendfile on;
  30. #高效传输
  31. #tcp_nopush on;
  32. #开启长连接
  33. keepalive_timeout 65;
  34. #开启压缩
  35. #gzip on;
  36. #包含网站的配置文件
  37. include /etc/nginx/conf.d/*.conf;
  38. #一个server表示一个网站
  39. server {
  40. #监听端口
  41. listen 80;
  42. #网站提供的域名
  43. server_name localhost;
  44. #字符集
  45. charset utf8;
  46. #匹配、控制访问的网站站点
  47. location / {
  48. #指定站点目录
  49. root /usr/share/nginx/html;
  50. #指定默认访问的页面
  51. index index.html index.htm;
  52. }
  53. }
  54. }
  55. access_log /var/log/nginx/access.log json ;

日志格式可以用下面的json格式,更加清晰明了:

  1. log_format json '{"@timestamp":"$time_iso8601",'
  2. '"host":"$server_addr",'
  3. '"service":"nginxTest",'
  4. '"trace":"$upstream_http_ctx_transaction_id",'
  5. '"log":"log",'
  6. '"clientip":"$remote_addr",'
  7. '"remote_user":"$remote_user",'
  8. '"request":"$request",'
  9. '"http_user_agent":"$http_user_agent",'
  10. '"size":$body_bytes_sent,'
  11. '"responsetime":$request_time,'
  12. '"upstreamtime":"$upstream_response_time",'
  13. '"upstreamhost":"$upstream_addr",'
  14. '"http_host":"$host",'
  15. '"url":"$uri",'
  16. '"domain":"$host",'
  17. '"xff":"$http_x_forwarded_for",'
  18. '"referer":"$http_referer",'
  19. '"status":"$status"}';

七、搭建小游戏案例

7.1 编写史上最简单配置

  1. [root@web01 ~]# vim /etc/nginx/conf.d/game.conf
  2. server {
  3. listen 80;
  4. server_name www.mario.com;
  5. #server_name localhost;
  6. location / {
  7. root /opt/Super_Marie;
  8. index index.html;
  9. }
  10. }

7.2 检查配置文件

  1. [root@web01 code]# nginx -t
  2. nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
  3. nginx: configuration file /etc/nginx/nginx.conf test is successful

7.3 创建站点目录

  1. [root@web01 ~]# mkdir -p /opt/Super_Marie

7.4 上传代码包

  1. [root@web01 ~]# cd /opt/Super_Marie
  2. [root@web01 Super_Marie]# rz
  3. [root@web01 Super_Marie]# unzip mario.zip

7.5 重载Nginx

  1. [root@web01 Super_Marie]# systemctl restart nginx

7.6 配置Windows下的hosts(域名解析)

  1. Windowshosts文件所在位置:
  2. C:\Windows\System32\drivers\etc\hosts
  3. 172.16.1.7 www.mario.com

7.7 浏览器上访问页面玩游戏

八、再搭建一个游戏

8.1 编辑配置文件

  1. [root@web01 ~]# vim /etc/nginx/conf.d/gametwo.conf
  2. server {
  3. listen 80;
  4. server_name www.tank.com;
  5. location / {
  6. root /opt/tank;
  7. index index.html;
  8. }
  9. }
  10. [root@web01 opt]# nginx -t
  11. nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
  12. nginx: configuration file /etc/nginx/nginx.conf test is successful
  13. [root@web01 ~]# mkdir -p /opt/tank
  14. [root@web01 opt]# systemctl restart nginx

8.2 上传代码

  1. [root@web01 ~]# cd /opt/tank
  2. [root@web01 tank]# rz
  3. [root@web01 tank]# unzip tankedazhan.zip

8.3 配置Windows下的hosts

  1. C:\Windows\System32\drivers\etc\hosts
  2. 172.16.1.7 www.mario.com www.tank.com

8.4 浏览器上输入不同域名地址访问对应页面进行游戏

原文链接:http://www.cnblogs.com/JZjuechen/p/15760844.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号