02架构 03.2 nginx 部署

部署

yum 安装

1、epol 源安装

yum -y install nginx

# 安装好了以后启动并设置开机启动
systemctl enable nginx
systemctl start nginx

# yum安装nginx的位置
[root@nfs01 ~]# find / -name nginx | xargs ls -ld
-rw-r--r-- 1 root  root     261 Oct 19 07:55 /etc/logrotate.d/nginx
drwxr-xr-x 4 root  root    4096 Nov 15 15:14 /etc/nginx
drwxr-xr-x 3 root  root      21 Nov 15 15:14 /usr/lib64/nginx
-rwxr-xr-x 1 root  root 1266632 Oct 19 07:58 /usr/sbin/nginx
drwxr-xr-x 4 root  root      33 Nov 15 15:14 /usr/share/nginx
drwxrwx--- 3 nginx root      17 Nov 15 15:14 /var/lib/nginx
drwx--x--x 2 root  root       6 Oct 19 07:58 /var/log/nginx

2、官方源安装

1、先登录nginx.org网站
http://nginx.org
2、右侧点 2019 【一般用半年或一年前的版本】
3、Ctrl+F,搜索stable【稳定版】,找到2个版本1.16.1、1.16.0,点开1.16.1
4、再点击最下方的 stable and mainline
5、我用的是Centos,所以再点击 RHEL/CentOS 
	To set up the yum repository, create the file named /etc/yum.repos.d/nginx.repo with the following contents:
	要设置 yum 存储库,请创建名为 /etc/yum.repos.d/nginx.repo 包括以下内容:
	复制 [nginx-stable] 下面的一段内容,根据提示将内容复制到 /etc/yum.repos.d/nginx.repo 文件中
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

题外话:gpgcheck是GPG验证是否开启的选项,1是开启,0是不开启,一般情况可以关掉。
GPG是加密和数字签名的免费工具,大多用于加密信息的传递。除了仅用密码加密外,GPG最大的不同是提供了“公钥/私钥”对。利用一方的“公钥”别人加密信息不再需要告诉密码,随时随地都能发送加密信息。而这种加密是单向的,只有一方的“私钥”能解开加密。数字签名又是另一大使用方向。通过签名认证,别人能确保发布的消息来自一方,而且没有经过修改。

	#如果在安装的过程中遇到需要依赖包就先安装依赖包,一般不会报错
yum -y install nginx

#安装好了以后启动并设置开机启动
systemctl enable nginx
systemctl start nginx

配置文件介绍

1、yum 安装后才可以用 rpm -ql nginx 显示 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			配置文件	Content-Type与扩展名

#4.Nginx管理相关命令
/usr/sbin/nginx					命令		 Nginx命令行管理终端工具
/usr/sbin/nginx-debug			命令		 Nginx命令行与终端调试工具

#4.Nginx日志相关目录与文件
/var/log/nginx					目录		 Nginx默认存放日志目录
/etc/logrotate.d/nginx			配置文件	Nginx默认的日志切割

server 层默认 配置文件

	#yum 安装后默认的 server 层配置文件
[root@web01 ~]# vim /etc/nginx/conf.d/default.conf
# server 层,控制用户访问网站获得的内容
server {
	#监听端口 80
    listen       80;
    #提供给用户访问的域名
    server_name  localhost;

	#支持的字符集 koi8-r ,支持多个字符集,就需要在utf-8外面加上 'utf-8,gbk'
    #charset koi8-r;
    #日志保存路径和保存内容【调用主配置main里面的设置】
    #access_log  /var/log/nginx/host.access.log  main;

	#location 层,控制访问资源路径,localhost:80/index.html = /usr/share/nginx/html/index.html
    location / {
    	#存放网站源代码的目录
        root   /usr/share/nginx/html;
        #默认返回网站的文件
        index  index.html index.htm;
    }

	# 用户请求出错后代码为 404 时,跳转至 /usr/share/nginx/html/404.html
    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    # 用户请求出错后代码为 5** 时,将服务器错误页重定向到静态页 /50x.html
    error_page   500 502 503 504  /50x.html;
    #精确匹配至 /50x.html 资源文件
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    # 用户请求的 URI 以 .php$ 结尾就跳转到 http://127.0.0.1 处理
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    # 用户请求的 URI 以 .php$ 结尾就传递给 PHP 处理,PHP 地址是 127.0.0.1:9000
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    # 如果Apache的文档根目录与nginx的文档根目录一致,则拒绝访问 .htaccess 文件 
    #如果用户请求的 URI 匹配到 .ht 则拒绝所有请求
    #location ~ /\.ht {
    #    deny  all;
    #}
}

源码安装

# 源码安装【编译安装|二进制安装】
	先登录nginx.org网站,Ctrl+F,搜索 stable 【稳定版】
	找到2个版本1.16.1、1.16.0,点开	nginx-1.16.1
	找到 nginx-1.16.1 下载它到Centos系统中并解压,解压时最好不要在挂载的NFS目录解压

---------------------------------------------------------------------------
[root@web01 code]# tar xf nginx-1.16.1.tar.gz
#在挂载的目录解压,过程中有可能会遇到错误提示,没有影响
	目录nginx-1.16.1:不能改变所有权为1001:操作不允许								
tar: nginx-1.16.1: Cannot change ownership to uid 1001, gid 1001: Operation not permitted
tar: Exiting with failure status due to previous errors
	由于前面的错误导致失败状态退出
---------------------------------------------------------------------------

[root@web01 ~]# tar xf nginx-1.16.1.tar.gz
[root@web01 ~]# cd nginx-1.16.1/

	#安装依赖包
yum install -y gcc gcc-c++ autoconf pcre pcre-devel make automake wget httpd-tools vim tree openssl openssl-devel zlib-devel

	#预编译【可以在其他安装好的主机上执行nginx -V,将内容复制到./configure 后面】

# 预编译时选择的模块越少编译的时间就越短,下面只添加了安装目录,模块目录,用户和用户组,过滤和验证模块
./configure --prefix=/usr/local/nginx-1.16.1 --modules-path=/usr/local/nginx-1.16.1/modules --user=www --group=www \
--with-http_addition_module --with-http_auth_request_module

# 只指定安装目录,也会让所有文件目录生成在安装目录下【也可以根据需要指定各个目录位置】
./configure --prefix=/usr/local/nginx-1.20.1 --modules-path=/usr/local/-1.20.1/modules --user=www --group=www --with-compat --with-debug --with-file-aio --with-google_perftools_module --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_degradation_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_image_filter_module=dynamic --with-http_mp4_module --with-http_perl_module=dynamic --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-http_xslt_module=dynamic --with-mail=dynamic --with-mail_ssl_module --with-pcre --with-pcre-jit --with-stream=dynamic --with-stream_ssl_module --with-stream_ssl_preread_module --with-threads --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -m64 -mtune=generic' --with-ld-opt='-Wl,-z,relro -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -Wl,-E'

################################## yum 安装时的选项,nginx -V 获取内容如下 ##################################
	#预编译选项含义参考网站
	https://blog.csdn.net/baidu_31247899/article/details/79480130
[root@web01 ~]# 2>&1 nginx -V | sed -r 's# #\n#g'| grep '^-'
--prefix=/etc/nginx											# nginx安装位置
--sbin-path=/usr/sbin/nginx 								# nginx二进制执行文件路径
--modules-path=/usr/lib64/nginx/modules 					# nginx模块目录
--conf-path=/etc/nginx/conf		 							# 配置文件存放位置
--error-log-path=/var/log/nginx/error.log 					# 错误日志文件位置
--http-log-path=/var/log/nginx/access.log 					# 访问日志文件位置
--pid-path=/var/run/nginx.pid 								# PID文件位置
--lock-path=/var/run/nginx.lock 							# sock文件【进程锁】位置
--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 FastCGI模块使用的临时文件路径
--http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp 			# 从uwsgi服务器接收到数据的临时文件存放的目录
--http-scgi-temp-path=/var/cache/nginx/scgi_temp 			# 从SCGI服务器接收到数据的临时文件存放的目录
--user=nginx 												# 默认用户
--group=nginx 												# 默认用户组
	#下面是模块
--with-compat					#动态模块的兼容性
--with-debug
--with-file-aio					#支持文件的异步IO,就是异步非阻塞
--with-google_perftools_module
--with-threads					#支持多线程
--with-http_addition_module		#过滤模块,它可以在回复正文前后加上内容,帮助搜索引擎收集页面信息
--with-http_auth_request_module	#验证模块,它允许您的nginx通过发送请求到后端服务器
--with-http_dav_module			#增加PUT DELETE MKCOL创建集合
--with-http_degradation_module
--with-http_flv_module			#支持流媒体功能,视频等
--with-http_gunzip_module		#为不支持“gzip”编码方法的客户端解压缩,
								#当需要压缩存储数据时,该模块将非常有用,以节省空间并降低I/O成本
--with-http_gzip_static_module	#支持压缩功能,数据压缩后浏览器会自动解压,主要为了节省带宽
--with-http_image_filter_module=dynamic
--with-http_mp4_module			#多媒体模块
--with-http_perl_module=dynamic
--with-http_random_index_module	#随机显示首页模块
--with-http_realip_module		#Nginx获取真实IP模块
--with-http_secure_link_module	#支持安全链接
--with-http_slice_module		#nginx中文文档
--with-http_ssl_module			#安全套接层
--with-http_stub_status_module	#支持子状态模块,显示nginx的状态
--with-http_sub_module			#nginx替换网站响应内容
--with-http_v2_module			#让web服务器支持http2版本,可能出现【如果 443 端口一个网站用了 http2,其他都会是 http2】
--with-http_xslt_module=dynamic
--with-mail						#启用POP3/IMAP4/SMTP代理模块支持
--with-mail_ssl_module			#启用ngx_mail_ssl_module支持
--with-pcre 
--with-pcre-jit
--with-stream					#负载均衡模块
--with-stream_realip_module		
--with-stream_ssl_module
--with-stream_ssl_preread_module
	#以上是模块,实际使用时需要什么模块添加什么模块即可,添加太多会导致nginx程序很大,运行速度变慢
	#我是先yum安装一次,然后 nginx -V 获取参数,再卸载后给源码安装使用参数
	#编译的时候结合处理器的优化参数,让编译能更快一些
		#设置C编译器参数
--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'

#参考文件:
https://blog.csdn.net/baidu_31247899/article/details/79480130
https://www.cnblogs.com/duguxiaobiao/p/9128811.html
https://blog.csdn.net/weixin_43458020/article/details/84423662
https://blog.csdn.net/yongcto/article/details/15339305

--modules-path=/usr/local/nginx-1.16.1/modules 					# nginx模块目录
# 注意,实测:
yum安装后模块目录为 /usr/lib64/nginx/modules 初始是空目录
源码安装后虽然指定了模块目录,但是不会生成
####################################################################

	#预编译后会在 nginx 目录中生成一个 Makefile 文件和 objs 目录,根据 Makefile 文件进行编译
make
	#将编译好的文件复制到【./configure预编译】时指定的目录中
make install
	#创建一个软链接,利于后期升级
ln -s /usr/local/nginx-1.16.1 /usr/local/nginx
	#设置使用system管理nginx【文件内容来自其官方源安装Nginx的配置文件】

#配置环境变量【可直接使用 nginx 命令】使环境变量立即生效【source】
echo "export PATH=/usr/local/nginx/sbin:$PATH" >> /etc/profile.d/pz.sh
source /etc/profile.d/pz.sh

#配置 systemd 管理 nginx
cat > /usr/lib/systemd/system/nginx.service << EOF
[Unit]
Description=nginx - high performance web server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop

[Install]
WantedBy=multi-user.target
EOF

#安装好了以后启动并设置开机启动
systemctl enable nginx
systemctl start nginx

#验证nginx服务启动成功
[root@web01 code]# netstat -lntp | grep nginx
tcp    0   0 0.0.0.0:80    0.0.0.0:*    LISTEN      10087/nginx: master

添加模块

#官方模块使用文档
http://nginx.org/en/docs/

nginx -V 可以查看原来编译时都带了哪些参数
原来的参数:
./configure --prefix=/usr/local/nginx-1.16.1 --modules-path=/usr/local/nginx/modules --user=www --group=www \
--with-http_addition_module --with-http_auth_request_module

添加的参数:
--with-compat --with-file-aio --with-threads ......等等

步骤如下:
1. 使用参数重新配置:
./configure --prefix=/usr/local/nginx-1.16.1 --modules-path=/usr/local/nginx/modules --user=www --group=www \
--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'

2. 编译【但一定不要 make install 覆盖安装】
make

3. 替换nginx二进制文件:
备份一下原命令
cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak
cp /root/nginx-1.16.1/objs/nginx /usr/local/nginx/sbin/

4. 恢复之前的编译
cp /usr/local/nginx/sbin/nginx.bak /usr/local/nginx/sbin/nginx

参考链接:
https://blog.csdn.net/upskychan/article/details/102515838

#注意:如果更改了安装位置,PID、日志文件位置,用户信息等就需要先停止nginx服务,才能复制编译后的nginx
#建议更改安装位置,文件位置等信息还是重新编译安装以防止出错
systemctl stop nginx
cp /root/nginx-1.16.1/objs/nginx /usr/local/nginx/sbin/
systemctl start nginx

升级

新版本必须是编译安装的
#原来是1.16.1版本,现在要升级为1.18.0

#1.安装依赖
[root@web02 ~]# yum install -y gcc gcc-c++ autoconf pcre pcre-devel make automake wget httpd-tools vim tree openssl openssl-devel zlib-devel

#2.下载新版 Nginx
[root@web02 ~]# wget http://nginx.org/download/nginx-1.18.0.tar.gz

#3.解压代码包
[root@web02 ~]# tar xf nginx-1.18.0.tar.gz 

#4.生成编译文件 Makefile
[root@web02 ~]# cd nginx-1.18.0
[root@web02 nginx-1.18.0]# ./configure --prefix=/usr/local/nginx-1.18.0 --modules-path=/usr/local/nginx/modules --user=www --group=www \
--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.编译安装
[root@web02 nginx-1.18.0]# make && make install

#6.软链接重新指向
[root@web02 nginx-1.18.0]# cd
[root@web02 ~]# rm -f /usr/local/nginx && ln -s /usr/local/nginx-1.18.0 /usr/local/nginx

#7.移动pid文件
[root@web02 ~]# mv /usr/local/nginx-1.16.1/logs/nginx.pid /usr/local/nginx-1.18.0/logs/

#8.重载nginx
[root@web02 ~]# systemctl reload nginx

启动

# 绝对路径启停 Nginx
	1.Nginx启动
[root@zls ~]# /usr/sbin/nginx
	2.Nginx停止
[root@zls ~]# /usr/sbin/nginx -s stop
	3.Nginx重载
[root@zls ~]# /usr/sbin/nginx -s reload

# systemctl 启停 Nginx
	1.Nginx启动
[root@zls ~]# systemctl start nginx
	2.Nginx停止
[root@zls ~]# systemctl stop nginx
	3.Nginx重启
[root@zls ~]# systemctl restart nginx
	4.Nginx重载
[root@zls ~]# systemctl reload nginx

# 绝对路径启停 Nginx 和 systemctl 启停 Nginx 绝对绝对不能混合使用
# 混合使用会报错,并且两种命令都不能起作用,只能 kill nginx 进程

# 启动 Nginx 并设置开机启动
systemctl start nginx
systemctl enable nginx

# 检查启动状态
systemctl status nginx
ps -ef | grep nginx
[root@web01 code]# netstat -lntp | grep nginx
tcp    0   0 0.0.0.0:80     0.0.0.0:*      LISTEN      10087/nginx: master

# Windows 解析
C:\Windows\System32\drivers\etc\hosts
10.0.0.31 www.web01.com
# 访问web页面查看
www.web01.com

配置文件介绍

1、源码安装文件介绍
#1.Nginx主配置文件
/usr/local/nginx/conf/nginx.conf		配置文件	Nginx 主配置文件

#2.Nginx代理相关参数文件
/usr/local/nginx/conf/fastcgi_params	配置文件	Fastcgi代理配置文件
/usr/local/nginx/conf/scgi_params		配置文件	scgi代理配置文件
/usr/local/nginx/conf/uwsgi_params		配置文件	uwsgi代理配置文件

#3.Nginx编码相关配置文件
/usr/local/nginx/conf/win-utf			配置文件	Nginx编码转换映射文件
/usr/local/nginx/conf/koi-utf			配置文件	Nginx编码转换映射文件
/usr/local/nginx/conf/koi-win			配置文件	Nginx编码转换映射文件
/usr/local/nginx/conf/mime.types		配置文件	Content-Type与扩展名

#4.Nginx管理相关命令
/usr/local/nginx/sbin/nginx				命令		 Nginx命令行管理终端工具

#4.Nginx日志相关目录与文件
/usr/local/nginx/logs					目录		 Nginx默认存放日志目录

2、源码安装文件

#nginx目录
[root@web01 ~]# ll /usr/local/nginx/
total 0
drwx------ 2 www  root ...... client_body_temp	# 客户端请求产生的临时文件路径
drwxr-xr-x 2 root root ...... conf				# 配置文件目录
drwx------ 2 www  root ...... fastcgi_temp	# HTTP FastCGI模块使用的临时文件路径
drwxr-xr-x 2 root root ...... html				# 默认页面和50X页面
drwxr-xr-x 2 root root ...... logs				# 日志目录
drwx------ 2 www  root ...... proxy_temp		# 代理存储临时文件路径
drwxr-xr-x 2 root root ...... sbin			# nginx二进制执行文件路径
drwx------ 2 www  root ...... scgi_temp 	# 从SCGI服务器接收到数据的临时文件存放的目录
drwx------ 2 www  root ...... uwsgi_temp	# 从uwsgi服务器接收到数据的临时文件存放的目录

#配置文件
[root@web01 ~]# ll /usr/local/nginx/conf
total 68
-rw-r--r-- 1 root root ...... fastcgi.conf		# Fastcgi 代理配置文件
-rw-r--r-- 1 root root ...... fastcgi.conf.default	# 备份文件,同上
-rw-r--r-- 1 root root ...... fastcgi_params	# Fastcgi 代理配置文件
-rw-r--r-- 1 root root ...... fastcgi_params.default# 备份文件,同上
-rw-r--r-- 1 root root ...... koi-utf		# Nginx编码转换映射文件
-rw-r--r-- 1 root root ...... koi-win		# Nginx编码转换映射文件
-rw-r--r-- 1 root root ...... mime.types	# 文件类型与扩展名
-rw-r--r-- 1 root root ...... mime.types.default	# 备份文件,同上
-rw-r--r-- 1 root root ...... nginx.conf	# Nginx 主配置文件
-rw-r--r-- 1 root root ...... nginx.conf.default	# 备份文件,同上
-rw-r--r-- 1 root root ...... scgi_params	# scgi代理配置文件
-rw-r--r-- 1 root root ...... scgi_params.default	# 备份文件,同上
-rw-r--r-- 1 root root ...... uwsgi_params	# uwsgi代理配置文件
-rw-r--r-- 1 root root ...... uwsgi_params.default	# 备份文件,同上
-rw-r--r-- 1 root root ...... win-utf		# Nginx编码转换映射文件

————————————————
fastcgi.conf 和 fastcgi_params 都是 Fastcgi 代理配置文件
fastcgi.conf 多一行
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

因为fastcgi_param指令是数组型的,和普通指令相同的是:内层替换外层;
和普通指令不同的是:当在同级多次使用的时候,是新增而不是替换。

如果在同级定义两次SCRIPT_FILENAME,那么它们都会被发送到后端,这可能会导致一些潜在的问题,为了避免此类情况,便引入了一个新的配置文件。

知识点来源:https://blog.csdn.net/qq_42303254/article/details/89503656
我后面文本中配置伪静态的时候是1.16版本,那时候这样配置
        #关联的站点目录和PHP文件名
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        #包含的fastcgi模块的一些内容
        include fastcgi_params;
现在建议这样配置
        include fastcgi.conf;
————————————————

#日志文件
[root@web01 ~]# ll /usr/local/nginx/logs
total 12
-rw-r--r-- 1 root root 1577 2021-11-15 13:41 access.log	# 访问日志
-rw-r--r-- 1 root root 1165 2021-11-15 13:41 error.log	# 错误日志
-rw-r--r-- 1 root root    6 2021-11-14 16:55 nginx.pid	# pid文件

#二进制执行文件
[root@web01 ~]# ll /usr/local/nginx/sbin
total 3856
-rwxr-xr-x 1 root root 3947304 2021-11-14 16:47 nginx

主配置文件

#下面的配置文件内容是以1.16.1版本yum安装后的主配置文件为例的

#设置一个不用登陆的统一用户
groupadd -g 666 www
useradd -u 666 -g 666 -Ms /sbin/nologin www

#简介
http {} 层下允许有多个 Server {} 层
Server {} 层下又允许有多个 location {} 层

http {}		主要用来解决用户的请求与响应
server {}	主要用来响应具体的某一个网站
location {}	主要用于匹配网站具体URL路径

#设置主配置文件
[root@web01 ~]# vim /etc/nginx/nginx.conf 
#------------------------------核心模块--------------------------------
	#用户原来是nginx,替换为前面设置的用户和用户组
#user  www www;
user  www;
worker_processes 1;      #启动的worker数量与cpu核心数一致即可,或者 auto
	#日志级别有:	debug/info/notice/warn/error/crit/alter/emerg
	#每个级别含义	调试 | 信息 | 通知 |警告| 错误| 临界| 改变| 紧急状态
	#设置日志级别为warn【表示会记录warn/error/crit/alter/emerg】
error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;

#------------------------------事件驱动模块-----------------------------
#表示核心模块中的每一个工作进程 worker_processes  1;同一时间可以进行1024次连接
events {
    worker_connections  1024;	#限制每个进程能处理多少个连接
}

#-----------------------------http内核模块-----------------------------
http {
	#包含在mime.types文件内的类型都会显示在网页中
    include       /etc/nginx/mime.types;
    #默认不包含在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"';

	#日志设置路径及包含内容为main中的内容
    access_log  /var/log/nginx/access.log  main;
    
	#优化部分【分别在动静分离的web服务器上设置,2种设置要分开】
    #文件高效传输,静态资源服务器建议打开
    sendfile            on;
    #【网页内容都拿到了,再一次性推送给用户】
    #tcp_nopush          on;

	#保持长连接的时间65秒限制【建议使用】
    keepalive_timeout  65;

	#为了快速寻找到相应MIME type,Nginx使用散列表来存储MIME type与文件扩展名。types_hash_bucket_size 设置了每个散列桶占用的内存大小。types_hash_max_size影响散列表的冲突率。types_hash_max_size越大,就会消耗更多的内存,但散列key的冲突率会降低,检索速度就更快。types_hash_max_size越小,消耗的内存就越小,但散列key的冲突率可能上升。
	types_hash_max_size 4096;

	#网页的内容是否进行压缩【图片压缩不明显,文字压缩效果才明显】
    #gzip  on;

	#多虚拟主机,包含每个网站的配置文件路径
	#/etc/nginx/conf.d/目录下的文件内容如果有冲突就会以文件名排名靠前的文件为准
    include /etc/nginx/conf.d/*.conf;
}

--------------------------------------
# 静态资源高效读取 on 开启 | off 关闭
Syntax: sendfile on | off;
Default: sendfile off;
Context: http, server, location, if in location

# 将多个包一次发送,用于提升网络传输效率,大文件推荐打开,需要开启sendfile才行
Syntax: tcp_nopush on | off;
Default: tcp_nopush off;
Context: http, server, location


# 提高网络传输实时性,需要开启keepalive,来一个包发一个包不等待
Syntax: tcp_nodelay on | off;
Default: tcpnodelay off;
Context: http, server, location
--------------------------------------
#组合使用
    sendfile       on;
    tcp_nopush     on;

#组合使用
    keepalive_timeout  65;
    tcp_nodelay	   on;
    
#tcp_nopush 和 tcp_nodelay 不能同时开启,但是1.20.1版本yum安装的配置文件中同时存在了,实际生产没同时用过。
#网上找到一个解说:
只能说看似矛盾,同时打开sendfile,tcp_nopush与tcp_nodelay时,针对资源发送nginx会参考如下:

1、确保数据包在发送给客户之前是已满的

2、对于最后一个数据包,tcp_nopush将被删除,允许TCP立即发送,没有延迟
--------------------------------------

#检测配置文件是否正确
[root@web01 ~]# nginx -t
nginx: the configuration file /usr/local/nginx-1.20.1/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx-1.20.1/conf/nginx.conf test is successful
#修改配置文件后需要重启
systemctl restart nginx

server 层自主 配置文件

	#创建网站代码目录及文件,并给他授权
mkdir -p /data/code/html
echo 'Nginx_Linux' > /data/code/html/index.html
chown -R www.www /data

#初学者可以删除默认server层配置文件,设置一个最简单的 server 层配置文件【文件名要以.conf结尾】

	#根据需要编辑一个新的server层配置文件
vim /usr/local/nginx/conf/server/web.conf
	#使用Server配置网站, 每个Server{}代表一个网站(简称虚拟主机)
	#一个文件中可以有多个server {} ,建议还是每个网站设置一个server {}
server {
	#监听端口, 默认80
    listen       80;
    #提供给用户访问的域名
    server_name  www.web01.com;

    #日志保存路径和保存内容【调用主配置main里面的设置】
    access_log /usr/local/nginx/logs/web01.com.log main;
            
    #控制网站访问路径
    location / {
    	#存放网站源代码的目录位置
        root   /data/code/html;
        #默认返回网站的文件
        index  index.html index.htm index.php;
    }
}

#检测配置文件是否正确
nginx -t
#修改配置文件后需要重启
systemctl restart nginx

#访问测试
http://www.web01.com/
如果报错就通过 nginx -t 的提示进行检查
tail /usr/local/nginx/logs/error.log

日志管理

1、日志的配置
vim /etc/nginx/nginx.conf
http {
	......
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    ......
}

我们在主配置文件中看到了上面这个配置,这是配置日志会搜集哪些内容
$remote_addr        # 记录上一层的IP地址
$remote_user        # 记录客户端用户名
$time_local         # 记录通用的本地时间【14/Dec/2019:17:28:37 +0800】
$time_iso8601       # 记录ISO8601标准格式下的本地时间【2019-12-14T17:28:37+08:00】
$request            # 记录请求的方法以及请求的http协议【GET /favicon.ico HTTP/1.1】
$status             # 记录请求状态码(用于定位错误信息)【404】
$body_bytes_sent    # 发送给客户端的资源字节数,不包括响应头的大小【645】
$bytes_sent         # 发送给客户端的总字节数
$msec               # 日志写入时间。单位为秒,精度是毫秒。
$http_referer       # 记录从哪个页面链接访问过来的【http://www.web01.com/】
$http_user_agent    # 记录客户端操作系统和浏览器相关信息
【Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36】
$http_x_forwarded_for #记录来访客户端IP地址并转给下一层主机
$request_length     # 请求的长度(包括请求行, 请求头和请求正文)。
$request_time       # 请求花费的时间,单位为秒,精度毫秒
# 注:如果Nginx位于负载均衡器,nginx反向代理之后, web服务器无法直接获取到客户端真实的IP地址。
# $remote_addr获取的是反向代理的IP地址。 反向代理服务器在转发请求的http头信息中,
# 增加x-forwarded-for信息,用来记录客户端IP地址和客户端请求的服务器地址。

server {
    listen 80;
    server_name code.oldboy.com;

    #将 code.oldboy.com 网站的访问日志记录至对应的目录,使用main格式
    access_log /var/log/nginx/code.oldboy.com.log main;
    location / {
        root /code;
    }

    #当有人请求改favicon.ico时,不记录日志
    location /favicon.ico {
        access_log off;
        return 200;
    }
}


2、日志的切割【每天都会有很多的日志,所以需要定时切割并备份】
有2种方法进行日志的切割,一种是编写备份脚本,再设置定时任务执行
还有一种就是用logrotate工具【具体信息可以百度搜索logrotate】
cat /etc/logrotate.d/nginx
#nginx日志路径
/var/log/nginx/*.log {
	    #每天备份【日志文件将按日轮循。其它可用值为'weekly','monthly'或者'yearly'】
        daily
        #忽略丢失日志【在日志轮循期间,任何错误将被忽略】
        missingok
        #相同日志保存个数【将存储52个归档日志。对于第53个归档,时间最久的归档将被删除。】
        rotate 52
        #压缩【使用gzip进行压缩,第一个】
        compress
        #延时压缩【总是与compress选项一起用,delaycompress选项指示logrotate不要将最近的归档压缩,压缩将在下一次轮循周期进行。这在你或任何软件仍然需要读取最新归档时很有用。】
        delaycompress
        #忽略空文件【如果日志文件为空,轮循不会进行】
        notifempty
        #备份文件权限   读写权限为 640  属主(nginx)属组(adm)【以指定的权限创建全新的日志文件,同时logrotate也会重命名原始日志文件】
        create 640 nginx adm
        #开始脚本标记
        sharedscripts
        #脚本内容
        postrotate
                if [ -f /var/run/nginx.pid ]; then
                        kill -USR1 `cat /var/run/nginx.pid`
                fi
        #结束脚本标记
        endscript
}

以上是标准模板,还可以增加
size=50M	日志大小可以增长到50M
dateext		以创建日期命名

#测试备份【因为有延时备份,所以第一次的备份并没有压缩】
logrotate -vf /etc/logrotate.d/nginx
[root@web01 ~]# ll /var/log/nginx/
total 16
-rw-r----- 1 www adm    0 Dec 14 18:35 access.log
-rw-r----- 1 www adm 2455 Dec 14 18:35 access.log.1
-rw-r----- 1 www adm  287 Dec 14 18:26 access.log.2.gz
-rw-r----- 1 www adm    0 Dec 14 18:35 error.log
-rw-r----- 1 www adm 1245 Dec 14 18:35 error.log.1
-rw-r----- 1 www adm  220 Dec 14 18:26 error.log.2.gz

部署 小游戏网站

下载游戏代码

#访问游戏下载页面,点击 Download 下载
https://sourceforge.net/projects/mmario/

配置游戏站点目录

mkdir /code/game
unzip -q mario.zip
mv html/ /code/mario
chown -R www.www /code

编辑配置文件

cat > /etc/nginx/conf.d/mario.game.com.conf << EOF
server {
    listen 80;
    server_name mario.game.com;

    location / {
    root /code/mario;
    index index.html;
    }
}
EOF

systemctl restart nginx

访问页面

# Windows 解析
C:\Windows\System32\drivers\etc\hosts
10.0.0.31 mario.game.com
# 访问web页面查看
mario.game.com

总结

这个游戏,不能修改关卡、攻击能力和人物命数
只能访问,不能进行修改,所以这是一个静态页面网站
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值