image

一.web服务器基本知识

  • WWW 是 World Wide Web 的缩写

  • URL :<协定>://<主机地址或主机名>[:port]/<目录资源>

  • 协议 http 80 明文 https 加密 (ssl)数字证书 443

# web 服务器实现方法:
iis         apache (lamp) 70%            nginx (lnmp)          lighty
中间件:microsoft:iis         ibm:webspere 40%         tomcat
oracle :(ba) weblogic 30%      susion middleware
静态网站: html
动态: asp jsp php cgi asp.net

1、web服务器软件

1、apache
网络模型
	select
	poll
	epoll

2、Nginx

官网:https://nginx.org/
软件:https://nginx.org/download/

[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

二、部署Nginx

  • 一般用yum安装就行

1、yum安装
    [root@web01 ~]# vim /etc/yum.repos.d/nginx.repo
    [root@web01 ~]# yum install nginx -y
	[root@web01 ~]# systemctl stop httpd
	[root@web01 ~]# systemctl start nginx

2、二进制安装

3、编译安装
	[root@web01 ~]#  wget https://nginx.org/download/nginx-1.20.2.tar.gz
	[root@web01 ~]# tar -xf nginx-1.20.2.tar.gz
	[root@web01 nginx-1.20.2]# ./configure
	[root@web01 nginx-1.20.2]# make
	[root@web01 nginx-1.20.2]# make install

1、平滑增加Nginx模块

  • 这个用编译安装才用的到,用yum安装就不需要这个

增加模块必须重新编译。
[root@web01 ~]# tar -xf nginx-1.20.2.tar.gz
[root@web01 ~]# cd nginx-1.20.2
[root@web01 nginx-1.20.2]#./configure  --with-http_ssl_module
[root@web01 nginx-1.20.2]#make 
[root@web01 nginx-1.20.2]#make install 

image

三、nginx命令

1、-v : 打印版本号
[root@web01 ~]# nginx -v
nginx version: nginx/1.20.2

2、-V : 打印版本号和配置项
[root@web01 ~]# nginx -V
nginx version: nginx/1.20.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx 

3、-t : 检查配置文件
[root@web01 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

4、-T : 测试配置文件并启动

5、-q :打印错误日志

6、-s : 操作进程
	stop	:停止
	quit	:退出
	reopen	:重启
	reload	:重载
7、-p : 指定nginx的工作目录
8、-e : 指定错误日志路径
9、-c : 指定配置文件的路径
10、-g : 设置一个全局的Nginx配置项
[root@web01 ~]# nginx -g 'daemon off;'

1、Nginx配置文件

全局配置和模块配置

1、全局配置
	1、user : 指定Nginx的启动用户
	2、worker_processes : 定义Nginx的worker进程数
		auto === CPU数量
	3、error_log : 错误日志路径
	4、pid : pid的存放文件路径
	5、events : 模块配置
		5.1、worker_connections :每一个worker进程最多同时接入多少个请求
		5.2、use : 指定Nginx的网络模型
	6、http : web服务的模块
		6.1、include : 加载外部的配置项
		6.2、default_type : 如果找不到文件的类型,则按照指定默认类型处理
		6.3、log_format : 定义日志格式
			log_format json '{"@timestamp":"$time_iso8601",'
                  '"host":"$server_addr",'
                  '"service":"nginxTest",'
                  '"trace":"$upstream_http_ctx_transaction_id",'
                  '"log":"log",'
                  '"clientip":"$remote_addr",'
                  '"remote_user":"$remote_user",'
                  '"request":"$request",'
                  '"http_user_agent":"$http_user_agent",'
                  '"size":$body_bytes_sent,'
                  '"responsetime":$request_time,'
                  '"upstreamtime":"$upstream_response_time",'
                  '"upstreamhost":"$upstream_addr",'
                  '"http_host":"$host",'
                  '"url":"$uri",'
                  '"domain":"$host",'
                  '"xff":"$http_x_forwarded_for",'
                  '"referer":"$http_referer",'
                  '"status":"$status"}';
    		access_log /var/log/nginx/access.log json ;
    	6.4、sendfile : 高效读取文件
    	6.5、keepalive_timeout : 长连接保持连接的
    		HTTP 1.0 短链接
    		HTTP 1.1 长连接
    	6.6、server : 网址模块
    		6.6.1、listen : 监听的端口
    		6.6.2、server_name : 定义域名
    		6.6.3、location : 访问路径
    			6.6.3.1、root : 指定网址路径
    			6.6.3.2、index : 指定网址的索引文件
配置选项 说明
--prefix nginx的安装目录,默认为/usr/local/nginx
--sbin-path nginx可执行文件路径,若没有设置则依赖于--prefix
--conf-path 设置nginx.conf配置文件路径,nginx启动时可以通过-c参数指定配置文件
--error-log-path 错误日志路径
--http-log-path http主请求日志文件
--pid-path 存放nginx进程的pid号
--lock-path 共享存储器互斥锁文件路径
--http-client-body-temp-path 客户端收到请求后,临时存放请求体目录
--http-proxy-temp-path 使用代理后,通过该项设置存放请求体路径
--http-fastcgi-temp-path 设置FastCGI临时文件的目录
--http-uwsgi-temp-path 设置uWSGI临时文件的目录
--http-scgi-temp-path 设置SCGI临时文件的目录
--user 指定nginx运行的用户
--group 指定nginx运行的用户组
--with-pcre 设置PCRE库的源码路径
--with-http_v2_module 用来支持 HTTP 2.0 的
--with-http_ssl_module 如果需要对流量进行加密,可以使用该选项,再URLs中开始部分将会是https(需要OpenSSL库)
image

pS:小游戏

1、上传代码

2、编辑配置文件
[root@web01 conf.d]# vim /etc/nginx/conf.d/game.conf 
server {
    listen 80;
    server_name game.test.com;
    location / {
        root /opt/Super_Marie;
        index index.html;
    }
}

3、测试配置文件是否正常
[root@web01 conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

4、重启Nginx
[root@web01 conf.d]# systemctl restart nginx 

5、域名解析
C:\Windows\System32\drivers\etc\hosts
172.16.1.7 game.test.com

image

posted on 2021-12-31 21:39  耿蜀黍  阅读(207)  评论(0编辑  收藏  举报