随笔 - 90,  文章 - 0,  评论 - 3,  阅读 - 15868

1.Nginx介绍

1.什么是Nginx
Nginx是一个开源且高性能、可靠的Http Web服务、代理服务。

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

2.为什么选择Nginx
	1.高性能、高并发
	通常情况下,单次请求会得到更快的响应,另一方面在高峰期(如数以万计的并发请求),Nginx可以比其他Web服务器更快的响应请求。
	2.高扩展性
	Nginx功能模块化。Nginx官方提供了非常多的优秀模块提供使用,这些模块都可以快速地增加和减少。
	3.高可靠性
	所谓的高可靠性,是指Nginx可以在服务器上持续不断的运行,可以为企业提供999 9999
	4.热部署
	热部署指的是在不停止服务的情况下升级Nginx。由于Nginx的master管理进程与worker工作进程的分离设计,使得N滚下能够在7*24小时不间断服务的前提下,升级Nginx的可执行文件。它也支持不停止服务更新配置、更换日志文件等功能。
	5.互联网公司都选择Nginx
	首先,因为Nginx技术成熟,具备企业最常使用的功能,如代理、代理缓存、负载均衡、静态资源、动静分离、Https、lnmp、lnmt等等
	其次使用Nginx同意技术栈,降低维护成本,同时降低技术更新成本。
	6.Nginx使用Epoll网络模型,而Apache采用的是Select网络模型
	Select:当用户发起一次请求,select模型就会进行一次遍历扫描,从而导致性能地下
	Epoll:当用户发起请求时,Epoll模型会直接进行处理,效率高。
	

1568634316265.png

2.常见的Web服务器

3.Nginx使用场景

静态资源服务


反向代理服务(代理缓存)

动静分离

负载均衡

Https

访问控制

限速、限带宽
限制连接数

4.Nginx 安装 配置 启动

第一种:源码安装
第二种:yum安装---->官方源
第三种:yum安装---->epel 

1.安装官方仓库源
[root@web01 ~]# cat /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

2.使用yum直接安装
[root@web01 ~]# yum install nginx -y

3.启动nginx
[root@web01 ~]# systemctl start nginx
[root@web01 ~]# systemctl enable nginx

PS:http与nginx用的都是80端口,当http起来时,nginx无法启动,启动nginx时要将http pkill掉

5.Nginx 配置文件

[root@web01 ~]# cat /etc/nginx/nginx.conf 

#全局配置
user  nginx;							#nginx进程的用户身份
worker_processes  1; 	 #nginx工作进程数量

error_log  /var/log/nginx/error.log warn;	#错误日志的路径 [警告级别才会记录]
pid        /var/run/nginx.pid;		#进程运行后,会产生一个pid


events {						#事件模型
    worker_connections  1024;	  #每个work能供支持的连接数
    use epoll;					#使用epoll网络模型(默认,可以不写)
}


http {							#接受用户的http请求
    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;					#启用压缩功能

    include /etc/nginx/conf.d/*.conf;	#包含哪些文件(为了显示主配置文件简洁,分开来写)
}


PS:Nginx中的http、server、location之间的关系

http		标签主要是用来解决用户的请求与响应。
server		标签主要是用来响应具体的某一网站。
location	标签主要是用于匹配网站具体URi路径。

http{} 层下允许有多个Server{},一个Server{} 下又允许有多个location{}

6.Nginx 搭建游戏网页

1.注释掉之前的默认网站
[root@web01 html]# cd /etc/nginx/conf.d/
[root@web01 conf.d]# gzip default.conf 

2.编写游戏网站Nginx配置文件
server{
		listen 80;			#该网站提供访问的端口
		server_name game.oldxu.com;		#访问该网站的域名
		location / {		#通用匹配uri
				root /code; #真是的网站路径
				index index.html #默认返回的页面
		}
}

3.根据Nginx的配置文件,初始化
[root@web01 conf.d]# mkdir /code

4.上传代码
[root@web01 conf.d]# cd /code/
[root@web01 code]# rz html5.zip
[root@web01 code]# unzip html5.zip

5.检测语法
[root@web01 code]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

6.重载服务
[root@web01 code]# systemctl restart nginx

7.配置域名解析
C:\Windows\System32\drivers\etc

在hosts中劫持ip地址

1568638404363.png

8.Nginx整体的访问流程

http://    game.oldxu.com     /      game/yibihua/index.html

请求的uri:		/game/yibihua/index.html
真实映射位置:   /code/game/yibihua/index.html

9.Nginx 搭建 多个游戏网站 ---> 虚拟主机

虚拟主机:  在一台服务器上运行多套网站
	
Nginx配置虚拟主机有如下三种方式:
	方式一:基于主机多ip方式	
	方式二:基于多端口的配置方式
	方式三:基于名称方式(多域名)
	
方式一、基于主机多IP方式
[root@web01 conf.d]# cat ip_eth0.conf 
server {
	listen 10.0.0.7:80;
	location / {
		root /ip1;
		index index.html;
	}
}
server {
	listen 172.16.1.7:80;
	location / {
		root /ip2;
		index index.html;
	}
}
[root@web01 conf.d]# mkdir /ip1 /ip2
[root@web01 conf.d]# echo "10...." > /ip1/index.html
[root@web01 conf.d]# echo "172...." > /ip2/index.html
[root@web01 conf.d]# systemctl restart nginx

测试访问
[root@web01 ~]# curl http://10.0.0.7
10.... 
[root@web01 ~]# curl http://172.16.1.7
172....


方式二、基于端口的配置方式	  81 82 83
	公司内部有多套系统,希望部署在一台服务器上, 而内网又没有域名.
	所以,我们可以通过相同IP,不同的端口,访问不同的网站页面.
[root@web01 conf.d]# cat port.conf 
server {
	listen 81;

	location / {
		root /81;
		index index.html;
	}
}

server {
	listen 82;

	location / {
		root /82;
		index index.html;
	}
}

server {
	listen 83;

	location / {
		root /83;
		index index.html;
	}
}
[root@web01 conf.d]# mkdir /81 /82 /83
[root@web01 conf.d]# echo "81" > /81/index.html
[root@web01 conf.d]# echo "82" > /82/index.html
[root@web01 conf.d]# echo "83" > /83/index.html

posted on   杨港澳  阅读(48)  评论(1编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
历史上的今天:
2019-12-24 第五章 存储引擎
2019-12-24 3.索引的应用范围

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示