部署
yum 安装
1、epol 源安装
yum -y install nginx
systemctl enable nginx
systemctl start nginx
[root@nfs01 ~]
-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 文件列表
/etc/nginx/nginx.conf 配置文件 Nginx 主配置文件
/etc/nginx/conf.d/default.conf 配置文件 默认网站配置文件
/etc/nginx/fastcgi_params 配置文件 Fastcgi代理配置文件
/etc/nginx/scgi_params 配置文件 scgi代理配置文件
/etc/nginx/uwsgi_params 配置文件 uwsgi代理配置文件
/etc/nginx/win-utf 配置文件 Nginx编码转换映射文件
/etc/nginx/koi-utf 配置文件 Nginx编码转换映射文件
/etc/nginx/koi-win 配置文件 Nginx编码转换映射文件
/etc/nginx/mime.types 配置文件 Content-Type与扩展名
/usr/sbin/nginx 命令 Nginx命令行管理终端工具
/usr/sbin/nginx-debug 命令 Nginx命令行与终端调试工具
/var/log/nginx 目录 Nginx默认存放日志目录
/etc/logrotate.d/nginx 配置文件 Nginx默认的日志切割
server 层默认 配置文件
[root@web01 ~]
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
源码安装
先登录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]
目录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 ~]
[root@web01 ~]
yum install -y gcc gcc-c++ autoconf pcre pcre-devel make automake wget httpd-tools vim tree openssl openssl-devel zlib-devel
./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'
https://blog.csdn.net/baidu_31247899/article/details/79480130
[root@web01 ~]
--prefix=/etc/nginx
--sbin-path=/usr/sbin/nginx
--modules-path=/usr/lib64/nginx/modules
--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
--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-debug
--with-file-aio
--with-google_perftools_module
--with-threads
--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
--with-mail_ssl_module
--with-pcre
--with-pcre-jit
--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'
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
yum安装后模块目录为 /usr/lib64/nginx/modules 初始是空目录
源码安装后虽然指定了模块目录,但是不会生成
make
make install
ln -s /usr/local/nginx-1.16.1 /usr/local/nginx
echo "export PATH=/usr/local/nginx/sbin:$PATH" >> /etc/profile.d/pz.sh
source /etc/profile.d/pz.sh
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
[root@web01 code]
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
systemctl stop nginx
cp /root/nginx-1.16.1/objs/nginx /usr/local/nginx/sbin/
systemctl start nginx
升级
新版本必须是编译安装的
[root@web02 ~]
[root@web02 ~]
[root@web02 ~]
[root@web02 ~]
[root@web02 nginx-1.18.0]
--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'
[root@web02 nginx-1.18.0]
[root@web02 nginx-1.18.0]
[root@web02 ~]
[root@web02 ~]
[root@web02 ~]
启动
1.Nginx启动
[root@zls ~]
2.Nginx停止
[root@zls ~]
3.Nginx重载
[root@zls ~]
1.Nginx启动
[root@zls ~]
2.Nginx停止
[root@zls ~]
3.Nginx重启
[root@zls ~]
4.Nginx重载
[root@zls ~]
systemctl start nginx
systemctl enable nginx
systemctl status nginx
ps -ef | grep nginx
[root@web01 code]
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 10087/nginx: master
C:\Windows\System32\drivers\etc\hosts
10.0.0.31 www.web01.com
www.web01.com
配置文件介绍
1、源码安装文件介绍
/usr/local/nginx/conf/nginx.conf 配置文件 Nginx 主配置文件
/usr/local/nginx/conf/fastcgi_params 配置文件 Fastcgi代理配置文件
/usr/local/nginx/conf/scgi_params 配置文件 scgi代理配置文件
/usr/local/nginx/conf/uwsgi_params 配置文件 uwsgi代理配置文件
/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与扩展名
/usr/local/nginx/sbin/nginx 命令 Nginx命令行管理终端工具
/usr/local/nginx/logs 目录 Nginx默认存放日志目录
2、源码安装文件
[root@web01 ~]
total 0
drwx------ 2 www root ...... client_body_temp
drwxr-xr-x 2 root root ...... conf
drwx------ 2 www root ...... fastcgi_temp
drwxr-xr-x 2 root root ...... html
drwxr-xr-x 2 root root ...... logs
drwx------ 2 www root ...... proxy_temp
drwxr-xr-x 2 root root ...... sbin
drwx------ 2 www root ...... scgi_temp
drwx------ 2 www root ...... uwsgi_temp
[root@web01 ~]
total 68
-rw-r--r-- 1 root root ...... fastcgi.conf
-rw-r--r-- 1 root root ...... fastcgi.conf.default
-rw-r--r-- 1 root root ...... fastcgi_params
-rw-r--r-- 1 root root ...... fastcgi_params.default
-rw-r--r-- 1 root root ...... koi-utf
-rw-r--r-- 1 root root ...... koi-win
-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
-rw-r--r-- 1 root root ...... nginx.conf.default
-rw-r--r-- 1 root root ...... scgi_params
-rw-r--r-- 1 root root ...... scgi_params.default
-rw-r--r-- 1 root root ...... uwsgi_params
-rw-r--r-- 1 root root ...... uwsgi_params.default
-rw-r--r-- 1 root root ...... win-utf
————————————————
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版本,那时候这样配置
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
现在建议这样配置
include fastcgi.conf;
————————————————
[root@web01 ~]
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
[root@web01 ~]
total 3856
-rwxr-xr-x 1 root root 3947304 2021-11-14 16:47 nginx
主配置文件
groupadd -g 666 www
useradd -u 666 -g 666 -Ms /sbin/nologin www
http {} 层下允许有多个 Server {} 层
Server {} 层下又允许有多个 location {} 层
http {} 主要用来解决用户的请求与响应
server {} 主要用来响应具体的某一个网站
location {} 主要用于匹配网站具体URL路径
[root@web01 ~]
user www;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
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;
keepalive_timeout 65;
types_hash_max_size 4096;
include /etc/nginx/conf.d/*.conf;
}
--------------------------------------
Syntax: sendfile on | off;
Default: sendfile off;
Context: http, server, location, if in location
Syntax: tcp_nopush on | off;
Default: tcp_nopush off;
Context: http, server, location
Syntax: tcp_nodelay on | off;
Default: tcpnodelay off;
Context: http, server, location
--------------------------------------
sendfile on;
tcp_nopush on;
keepalive_timeout 65;
tcp_nodelay on;
只能说看似矛盾,同时打开sendfile,tcp_nopush与tcp_nodelay时,针对资源发送nginx会参考如下:
1、确保数据包在发送给客户之前是已满的
2、对于最后一个数据包,tcp_nopush将被删除,允许TCP立即发送,没有延迟
--------------------------------------
[root@web01 ~]
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
vim /usr/local/nginx/conf/server/web.conf
server {
listen 80;
server_name www.web01.com;
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
$remote_user
$time_local
$time_iso8601
$request
$status
$body_bytes_sent
$bytes_sent
$msec
$http_referer
$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
$request_length
$request_time
server {
listen 80;
server_name code.oldboy.com;
access_log /var/log/nginx/code.oldboy.com.log main;
location / {
root /code;
}
location /favicon.ico {
access_log off;
return 200;
}
}
2、日志的切割【每天都会有很多的日志,所以需要定时切割并备份】
有2种方法进行日志的切割,一种是编写备份脚本,再设置定时任务执行
还有一种就是用logrotate工具【具体信息可以百度搜索logrotate】
cat /etc/logrotate.d/nginx
/var/log/nginx/*.log {
daily
missingok
rotate 52
compress
delaycompress
notifempty
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 ~]
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
部署 小游戏网站
下载游戏代码
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
访问页面
C:\Windows\System32\drivers\etc\hosts
10.0.0.31 mario.game.com
mario.game.com
总结
这个游戏,不能修改关卡、攻击能力和人物命数
只能访问,不能进行修改,所以这是一个静态页面网站