首页 > 技术文章 > Nginx Web快速入门—基础

dsryyds 2021-08-07 11:29 原文

Nginx Web快速入门—基础

Nginx的概述

Nginx是什么

## Nginx是一个开源且高性能、可靠的Http Web服务、代理服务

开源:直接获取源代码,GPL协议(GPL协议:开源组织规定:只要软件打上GPL这个标签,软件一律开源不收费,可以随意修改源代码,二次开发后必须开源出来,不收费)

高性能:支持海量并发(并发是同一时刻的访问量)

可靠:服务稳定

静态Web软件

nginx:官方
apache
IIS
lighttpd
tengine:第三方开发(阿里,淘宝开发)
openresty-nginx:第三方开发,支持LUA语言

动态的Web软件

Tomcat:javar的容器
Resin:javar的容器
weblogic:收费,稳定 
Jboss

选择Nginx的原因

Nginx非常轻量

功能模块少 (源代码仅保留http与核心模块代码,其余不够核心代码会作为插件来安装)
代码模块化 (易读,便于二次开发,对于开发人员非常友好)

互联网公司都选择Nginx

1.Nginx技术成熟,具备的功能是企业最常使用而且最需要的
2.适合当前主流架构趋势, 微服务、云架构、中间键
3.统一技术栈, 降低维护成本, 降低技术更新成本(防火墙,负载均衡,web,都可以是nginx来做)

Nginx采用Epool网络模型,Apache采用Select模型

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

Nginx应用场景

静态服务 代理服务 安全服务 流行架构
浏览器缓存 协议类型 访问控制 Nginx+PHP(Fastcgi_pass)LNMP
防资源盗用 正向代理 访问限制 Nginx+Java(Proxy_Pass)LNMT
资源分类 反向代理 流量限制 Nginx+Vue+Python(uwsgi_Pass)
资源压缩 负载均衡 拦截攻击
资源缓存 代理缓存 拦截异常请求
跨越访问 动静分离 拦截SQL注入

源码安装Nginx

Nginx的官方网站:TP
  • 1.获取nginx的源码包
[root@web02 ~]# mkdir /source_code
[root@web02 ~]# cd /source_code/
[root@web01 source_code]# wget https://nginx.org/download/nginx-1.20.1.tar.gz
[root@web02 /source_code]# ll
-rw-r--r-- 1 root root 1061461 May 25 23:34 nginx-1.20.1.tar.gz
  • 2.安装nginx的依赖包
[root@web02 /source_code]# yum install -y zlib-devel
[root@web02 /source_code]# yum install -y pcre-devel
  • 3.解压源码包
[root@web02 /source_code]# tar xf nginx-1.20.1.tar.gz 
[root@web02 /source_code]# ll
drwxr-xr-x 8 1001 1001     158 May 25 20:35 nginx-1.20.1
-rw-r--r-- 1 root root 1061461 May 25 23:34 nginx-1.20.1.tar.gz
  • 4.生成Mkefile自定义文件(定制化需求)
[root@web02 /source_code/nginx-1.20.1]# mkdir /app
[root@web02 /source_code/nginx-1.20.1]# ./configure --prefix=/app/nginx-1.20.1
[root@web02 /source_code/nginx-1.20.1]# ll
drwxr-xr-x 6 1001 1001    326 Jul 16 00:54 auto         
-rw-r--r-- 1 1001 1001 311503 May 25 20:35 CHANGES
-rw-r--r-- 1 1001 1001 475396 May 25 20:35 CHANGES.ru
drwxr-xr-x 2 1001 1001    168 Jul 16 00:54 conf          ##nginx配置文件
-rwxr-xr-x 1 1001 1001   2590 May 25 20:35 configure     ##生成
drwxr-xr-x 4 1001 1001     72 Jul 16 00:54 contrib
drwxr-xr-x 2 1001 1001     40 Jul 16 00:54 html
-rw-r--r-- 1 1001 1001   1397 May 25 20:35 LICENSE
-rw-r--r-- 1 root root    442 Jul 16 02:56 Makefile
drwxr-xr-x 2 1001 1001     21 Jul 16 00:54 man
drwxr-xr-x 3 root root    125 Jul 16 02:56 objs
-rw-r--r-- 1 1001 1001     49 May 25 20:35 README
drwxr-xr-x 9 1001 1001     91 Jul 16 00:54 src          ##源码存放目录

  • 5.编译(按照Makefile编译)
[root@web02 /source_code/nginx-1.20.1]# make
  • 6安装
[root@web02 /source_code/nginx-1.20.1]# make install
[root@web02 /app/nginx-1.20.1]# ll
drwxr-xr-x 2 root root 333 Jul 16 03:05 conf     ## 配置文件
drwxr-xr-x 2 root root  40 Jul 16 03:05 html     ## 站点目录
drwxr-xr-x 2 root root   6 Jul 16 03:05 logs     ##日志文件
drwxr-xr-x 2 root root  19 Jul 16 03:05 sbin     ##启动的命令
  • 7.安装后做软链接
[root@web02 /app/nginx-1.20.1]# ln -s /app/nginx-1.20.1/ /app/nginx
[root@web02 /app]# ll
lrwxrwxrwx 1 root root 18 Jul 16 03:14 nginx -> /app/nginx-1.20.1/
drwxr-xr-x 6 root root 54 Jul 16 03:05 nginx-1.20.1
  • 8.添加nginx命令的环境变量
[root@web02 /app]# vim /etc/profile
PATH="/app/nginx/sbin:$PATH"

[root@web02 /app]# echo $PATH
/app/nginx/sbin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
  • 9.加载环境变量
[root@web02 /app]# source /etc/profile
  • 10.检测配置文件
[root@web02 /app]# nginx -t
nginx: the configuration file /app/nginx-1.20.1/conf/nginx.conf syntax is ok
nginx: configuration file /app/nginx-1.20.1/conf/nginx.conf test is successful
  • 11.启动nginx服务
[root@web02 /app]# nginx

## 停止nginx
[root@web02 /app]# nginx -s stop
[root@web02 /app]# ps -ef|grep [n]ginx
root      11728   7926  0 03:41 pts/1    00:00:00 grep --color=auto nginx

## 重新加载
[root@web02 /app]# nginx -s reload
  • 12.检测nginx进度
[root@web02 /app]# ps -ef|grep [n]ginx
root      11689      1  0 03:32 ?        00:00:00 nginx: master process nginx
nobody    11690  11689  0 03:32 ?        00:00:00 nginx: worker process
  • 13.检测nginx的端口
[root@web02 /app]# netstat -lntup|grep 80
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      11689/nginx: master 

打开浏览器:http://10.0.0.8

image

Yum安装nginx

官网找安装nginx源的配置信息

image
image
image

  • 1.添加nginx的官方源
[root@web02 /app]# vim /etc/yum.repos.d/nginx.repo

[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
  • 2.安装nginx
[root@web02 /etc/yum.repos.d]# yum install -y nginx
  • 检查版本
[root@web02 /etc/yum.repos.d]# /sbin/nginx -v
nginx version: nginx/1.20.1
  • 查看安装nginx的功能
[root@web02 /etc/yum.repos.d]# /sbin/nginx -V

 --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'

Nginx相关配置文件

1.Nginx主配置文件

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

2.Nginx代理相关参数文件

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

3.Nginx编码相关配置文件

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

4.Nginx管理相关命令

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

5.Nginx日志相关目录与文件

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

Nginx的配置文件讲解

[root@web02 ~]# vim /etc/nginx/nginx.conf 
-------------------------- 核心模块 -------------------------
## 服务的启动用户
user nginx;
## worker进程(子进程),根据cpu的核心数
worker_processes auto;
## 错误日志,以及日志的级别
error_log /var/log/nginx/error.log notice;
## pid文件的存放路径
pid /var/run/nginx.pid;
-------------------------------------------------------------



----------------------- 事件驱动模块 -------------------------
events {
## 每个worker进程最大连接数
worker_connections 1024;
}
-------------------------------------------------------------



------------------------ HTTP网站配置 ----------------------------
http {
     server {
   }
   ## 默认nginx支持的文件类型
   include /etc/nginx/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 /var/log/nginx/access.log main;

    ## 高效文件传输
    sendfile on;
    #tcp_nopush on;

    ## 长链接超时时间
    keepalive_timeout 65;
    
    ## 传输过程中压缩
    #gzip on;

    ## 虚拟主机相关配置(网站的配置)
    server {    
         ## 监听在80端口
         listen   80;         
         ## IPv6
         listen [::]:80;    
         ## 配置访问的域名或者IP
         server_name _;
         # 该网站的站点目录
         root /usr/share/nginx/html;               
         ## 404页面的报错路径
         error_page 404 /404.html;    
         ## 404URL
         location = /404.html {
         }
         ## 5xx页面的报错路径
         error_page 500 502 503 504 /50x.html;
         ## 5xxURL
         location = /50x.html {
         } 
      }        
        ## 包含所有nginx的虚拟主机配置文件
        include /etc/nginx/conf.d/*.conf;      
}    
-------------------------------------------------------------

推荐阅读