Nginx 介绍,三种安装方式,相关文件,虚拟主机方式(多站点),简单排错‘。epel和centos-7下载

Nginx web基础

一、Nginx介绍

1.概述

Nginx是一个开源且高性能、可靠的,基于HTTP协议和反向代理的WEB服务器。同时还支持IMAP/POP3/SMTP服务(email)

开源: 直接获取源代码
高性能: 支持海量并发
可靠: 服务稳定

http://nginx.org/

2.Nginx特点

1)高性能,高并发

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

2)轻量且高扩展性

#轻量
功能模块少,只保留核心模块,其他代码模块化 (易读,便于二次开发,对于开发人员非常友好)

#高扩展性
需要什么模块再安装模块,不需要全部安装,并且还支持第三方模块

3)高可靠性

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

4)支持热部署

nginx可以再运行期间,更新迭代,代码部署

5)大多数公司都在用nginx

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

6)Nginx使用的是Epool网络模型

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

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
4. nginx应用场景

二、Nginx安装

1.安装方式

1.epol源安装
2.官方源安装
3.源码包安装

2.epol源安装

#先看有没有epel源
[root@web01 ~]# cd /etc/yum.repos.d/
[root@web01 yum.repos.d]# ll
total 8
-rw-r--r--. 1 root root 1775 Apr 15 03:50 CentOS-Base.repo

#要下载epel源
[root@web01 yum.repos.d]# curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

[root@web01 yum.repos.d]# curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo

#直接下载nginx
[root@web01 ~]# yum install -y nginx

3.官方源安装

1)配置官方源
# http://nginx.org/en/linux_packages.html#RHEL-CentOS
#nginx.org ---> documentation ---> Installing nginx ---> packages --->   RHEL/CentOS

[root@web01 ~]# 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

[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
2)安装依赖
yum install -y gcc gcc-c++ autoconf pcre pcre-devel make automake wget httpd-tools vim tree
3)安装nginx
[root@web01 ~]# yum install -y nginx
4)配置nginx
[root@web01 ~]# vim /etc/nginx/nginx.conf
user  www;  # 必须要有这个id:www,再根据需要设置属主属组权限
5)启动服务
1.方法一:
[root@web01 ~]# systemctl start nginx

2.方法二:
[root@web01 ~]# nginx
6)检查启动
1.方式一
[root@web01 ~]# systemctl status nginx

2.方式二:
[root@web01 ~]# ps -ef | grep nginx

3.方式三:
[root@web01 ~]# netstat -lntp | grep 80

4.方式四:
直接访问网站  http://192.168.15.7/

5.方式五
[root@web01 ~]# curl 192.168.15.7

6.方式六:
[root@web01 ~]# nginx -v
7)nginx常用命令
1.nginx启动
    1)方法一:
    [root@web01 ~]# systemctl start nginx
    2)方法二:
    [root@web01 ~]# nginx
    
#注意:使用什么方式启动的,就使用对应的方式关闭
2.nginx停止
    1)方法一:
    [root@web01 ~]# systemctl stop nginx
    2)方法二:
    [root@web01 ~]# nginx -s stop
    
3.nginx重启
	1)方法一:
	[root@web01 ~]# systemctl restart nginx
	
4.nginx重载,重新加载配置文件
	1)方法一:
	[root@web01 ~]# systemctl reload nginx
	2)方法二:
	[root@web01 ~]# nginx -s reload
	
5.加入开机自启
	[root@web01 ~]# systemctl enable nginx
	
#Centos6:
	启动:nginx
		service nginx start
		/etc/init.d/nginx start
	加入开机自启:
		chkconfig nginx on

4.源码包安装

1)下载安装包
# http://nginx.org/en/download.html
nginx.org ---> download ---> 下载Mainline version

[root@web03 opt]# wget http://nginx.org/download/nginx-1.16.10.tar.gz
2)解压
[root@web3 opt]# tar -xf nginx-1.16.10.tar.gz 
3)创建用户
[root@web03 ~]# groupadd www -g 678
[root@web03 ~]# useradd www -u 678 -g 678 -s /sbin/nologin -M
4) 生成
# 检查并设置系统
[root@web3 nginx-1.16.10]# yum install pcre pcre-devel perl-devel perl-ExtUtils-Embed -y

# 设置参数并检查系统
[root@web3 nginx-1.16.10]# ./configure --user=www --group=www --prefix=/usr/local/nginx-1.16.10   --with-stream    --with-mail        --with-http_perl_module      --without-http_gzip_module     --with-http_ssl_module 
#  如果报错没有哪个模块,如zlib,就要安装
yum install -y zilb zlib-devel     --包和依赖
再执行命令
./configure 
5)编译安装(编译的时候需要内存比较多)
[root@web3 nginx-1.19.10]# make -j && make install   # -j:多核编译
6)做软连接 (可做可不做,为了以后升级方便)
[root@web03 nginx-1.16.1]# ln -s /usr/local/nginx-1.16.1 /usr/local/nginx
7)配置环境变量
[root@web03 ~]# vim /etc/profile
# 最后一行添加
export PATH=$PATH:/usr/local/nginx-1.16.10/sbin

[root@web03 ~]# source /etc/profile    # 加载文件
8)启动nginx
#用二进制安装的nginx,启动时没有办法使用system管理,需要我们自己配置
[root@web03 ~]# vim /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target
[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID
[Install]
WantedBy=multi-user.target

#启动
[root@web03 ~]# systemctl daemon-reload   #重载
[root@web03 ~]# systemctl start nginx
9)nginx升级
1.下载新版本的包
[root@web02 ~]# wget http://nginx.org/download/nginx-1.19.0.tar.gz

2.解压
[root@web02 ~]# tar xf nginx-1.19.0.tar.gz

3.生成
[root@web02 ~]# cd nginx-1.19.0
[root@web02 nginx-1.19.0]# ./configure --prefix=/usr/local/nginx-1.19.0 --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

4.编译安装
[root@web02 nginx-1.19.0]# make -j && make install

5.重做软连接
[root@web02 ~]# rm -rf /usr/local/nginx && ln -s /usr/local/nginx-1.19.0 /usr/local/nginx

6.重启服务
[root@web02 ~]# systemctl restart nginx
10) nginx客户端参数详解
# 基本命令
nginx   -h : 帮助信息
		-v : 展示版本信息
		-V :展示版本信息及模块配置信息
		-t : 检查配置文件是否正确
		-q : 指定配置项,并启动
		-s : 指定nginx启动方式
		-c : 指定配置文件路径
		-e : 指定错误日志路径
		-p :指定nginx安装路径

三 Nginx相关文件

#查看nginx相关配置⽂件
[root@web01 ~]# rpm -ql nginx
1 . Nginx主配置⽂件
nginx主配置文件: /etc/nginx/nginx.conf       		       
默认⽹站配置⽂件: /etc/nginx/conf.d/default.conf 	  	
2.Nginx代理相关参数⽂件
Fastcgi代理配置⽂件(php): /etc/nginx/fastcgi_params
scgi代理配置⽂件: /etc/nginx/scgi_params
uwsgi代理配置⽂件(python): /etc/nginx/uwsgi_params
3.Nginx编码相关配置⽂件
Nginx编码转换映射⽂件: /etc/nginx/win-utf
Nginx编码转换映射⽂件: /etc/nginx/koi-utf
Nginx编码转换映射⽂件: /etc/nginx/koi-win
Content-Type与扩展名: /etc/nginx/mime.types
4.Nginx管理相关命令
Nginx命令⾏管理终端⼯具: /usr/sbin/nginx
Nginx命令⾏与终端调试⼯具: /usr/sbin/nginx-debug
5.Nginx⽇志相关⽬录与⽂件
Nginx默认存放⽇志⽬录: /var/log/nginx
Nginx默认的⽇志切割: /etc/logrotate.d/nginx

Nginx配置⽂件

# 配置文件/etc/nginx/nginx.conf
# yum安装:/etc/nginx
# 源码包安装:安装家目录的conf文件夹中
# ---------- 全局配置(全局生效) ------------------
user  nginx;			# 启动nginx work进程的用户名
worker_processes  auto;		# 启动的worker进程数(auto默认跟cpu数量相同)

error_log  /var/log/nginx/error.log notice;	# 错误日志路径
pid        /var/run/nginx.pid;			# PID 文件路径

# ---------- 系统事件配置模块(全局生效)-----------------------
events {				# 事件配置模块
    worker_connections  1024;		# 最大连接数
    use epool;				# 指定网络模型(select、pool、epool)
}

# ---------- HTTP 请求模块(处理HTTP请求的模块)
http {					# 模块名称	
    include       /etc/nginx/mime.types;	# nginx可以处理的文件类型
    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;	# 访问日志

	# TCP连接配置
    sendfile        on;
    #tcp_nopush     on;

	# 长链接配置
    keepalive_timeout  65;

	# gzip压缩
    #gzip  on;

	# 包含其他文件
    include /etc/nginx/conf.d/*.conf;
    
    server { # 每一个Server就是一个网站
        listen       80;		# 监听的端口
        server_name  localhost;		# 域名

        #access_log  /var/log/nginx/host.access.log  main; # 访问日志

        location / {				# 位置(指定访问的站点)
            root   /usr/share/nginx/html;		# 指定的站点目录
            index  index.html index.htm;		# 指定索引文件
        }
    }
}

五 Nginx的站点

虚拟主机方式

web网站,nginx是支持多站点的服务的
1.基于多IP的方式
2.基于多端口的方式
3.基于多域名的方式

案例

  • Nginx基于域名代理多个站点
# 在xshell写
[root@web02 ~]# cd  /usr/share/nginx

[root@web02 nginx]# mkdir web01 && mkdir web02

[root@web02 nginx]# vim web01.conf
server {                                    #模块
     listen 80;             #监听80端口
	server_name www.web01.com;
	location / {              #所有的访问都走这里
		root /usr/share/nginx/web01/;          #指定站点目录
		index index.html;	       #指定索引文件
	}  
}

[root@web02 nginx]# vim web02.conf
server {
	listen 80;
	server_name www.web02.com;
	location / {
		root /usr/share/nginx/web02/;
		index index.html;
	}
}

[root@web02 nginx]# nginx -t    #检测
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

[root@web02 nginx]# systemctl restart nginx  # 重启

#修改windows的hosts文件
C:WindowsSystem32driversetchosts
末行写 192.168.15.8 www.web01.com www.web02.com

  • Nginx基于IP的多个站点
[root@web02 nginx]# vim web01.conf
server {
	listen 80;
	server_name 192.168.1.8:80;
	location / {
		root /usr/share/nginx/web01/;
		index index.html;	
	}
}

[root@web02 nginx]# vim web02.conf
server {
	listen 80;
	server_name 172.16.1.8:80;
	location / {
		root /usr/share/nginx/web02/;
		index index.html;
	}
}
  • 基于多端口的多站
    [root@web02 nginx]# vim web01.conf
    server {
    	listen 8080;
    	server_name localhost;
    	location / {
    		root /usr/share/nginx/web01/;
    		index index.html;	
    	}
    }
    
    [root@web02 nginx]# vim web02.conf
    server {
    	listen 8090;
    	server_name localhost;
    	location / {
    		root /usr/share/nginx/web02/;
    		index index.html;
    	}
    }
    

六 Nginx排错

[root@web02 ~]# systemctl status nginx -l

原文地址:https://www.cnblogs.com/caodan01/p/14707524.html