2.lamp-----nginx常见配置

一、并发优化

将nginx复制到server3

[root@server1 ~]# cd nginx-1.20.1/
[root@server1 nginx-1.20.1]# ls
auto     CHANGES.ru  configure  html     Makefile  objs    src
CHANGES  conf        contrib    LICENSE  man       README
[root@server1 nginx-1.20.1]# cd /usr/local
[root@server1 local]# ls
bin  etc  games  include  lib  lib64  libexec  nginx  php  sbin  share  src
[root@server1 local]# scp -r nginx/ server3:/usr/local		#复制到server3

在这里插入图片描述

server3操作

[root@server3 ~]# cd /usr/local
[root@server3 local]# ls
bin  games    lib    libexec  sbin   src
etc  include  lib64  nginx    share
[root@server3 local]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/	#配置快速启动,环境变量问题配置完成,可直接在命令行启动nginx

[root@server3 ~]# cd /usr/local/nginx/
[root@server3 nginx]# ls
client_body_temp  fastcgi_temp  logs        sbin       uwsgi_temp
conf              html          proxy_temp  scgi_temp
[root@server3 nginx]# cd conf/
[root@server3 conf]# ls
fastcgi.conf            koi-win             scgi_params
fastcgi.conf.default    mime.types          scgi_params.default
fastcgi_params          mime.types.default  uwsgi_params
fastcgi_params.default  nginx.conf          uwsgi_params.default
koi-utf                 nginx.conf.default  win-utf
[root@server3 nginx]# useradd nginx	#创建用户nginx
[root@server3 nginx]# nginx -t	#语法检测
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@server3 nginx]# nginx	#启动nginx
[root@server3 nginx]# nginx -s reload
[root@server3 nginx]# cd conf
[root@server3 conf]# ls
fastcgi.conf            koi-win             scgi_params
fastcgi.conf.default    mime.types          scgi_params.default
fastcgi_params          mime.types.default  uwsgi_params
fastcgi_params.default  nginx.conf          uwsgi_params.default
koi-utf                 nginx.conf.default  win-utf
[root@server3 conf]# vim nginx.conf	#修改默认发布 

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

server2操作

[root@server2 ~]# yum install httpd	#安装httpd服务
[root@server2 ~]# systemctl enable  --now http
[root@server2 ~]# firewall-cmd --permanent --add-service=http
success
[root@server2 ~]# firewall-cmd --reload 

[root@server1 ~]# cd /usr/local/nginx/conf/
[root@server1 conf]# ls
fastcgi.conf            koi-win             scgi_params
fastcgi.conf.default    mime.types          scgi_params.default
fastcgi_params          mime.types.default  uwsgi_params
fastcgi_params.default  nginx.conf          uwsgi_params.default
koi-utf                 nginx.conf.default  win-utf
[root@server1 conf]# vim nginx.conf
[root@server1 conf]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@server1 conf]# nginx -s reload

[root@server2 ~]# cd /var/www/html/
[root@server2 html]# ls
[root@server2 html]# echo server2 > index.html

[root@server3 ~]# cd /usr/local/nginx/html/
[root@server3 html]# ls
50x.html  index.html  index.php
[root@server3 html]# echo server3 > index.html 
[root@server3 html]# nginx -s reload	#重新启动ngnix
[root@server3 html]# systemctl  disable --now firewalld	#关闭防火墙

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

真机查看

vim /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
172.25.15.1 www.westos.org

[root@foundation15 ~]# curl www.westos.org
server2
[root@foundation15 ~]# curl www.westos.org
server3

在这里插入图片描述

二、nginx平滑升级

1.安装低版本

[root@server1 ~]# ls
nginx-1.19.4.tar.gz               
nginx-1.20.1.tar.gz      
[root@server1 ~]# tar zxf nginx-1.19.4.tar.gz 
[root@server1 ~]# cd nginx-1.19.4/
[root@server1 nginx-1.19.4]# ls
auto     CHANGES.ru  configure  html     man     src
CHANGES  conf        contrib    LICENSE  README
[root@server1 nginx-1.19.4]# vim auto/cc/gcc 
171 # debug
172 #CFLAGS="$CFLAGS -g"
173 
174 # DragonFly's gcc3 generates DWARF
175 #CFLAGS="$CFLAGS -g -gstabs"
[root@server1 nginx-1.19.4]# ./configure --prefix=/opt/nginx	#编译源码
[root@server1 nginx-1.19.4]# make 
[root@server1 nginx-1.19.4]# make install	#安装

备份原文件
[root@server1 nginx-1.19.4]# cd /opt/nginx/sbin/
[root@server1 sbin]# ls
nginx
[root@server1 sbin]# cp nginx nginx.old
[root@server1 sbin]# ls
nginx  nginx.old

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

2.解压新版本只做make不make install

[root@server1 ~]# tar zxf nginx-1.20.1.tar.gz
[root@server1 ~]# ls
mysql-5.7.31               oniguruma-6.8.2-1.el7.x86_64.rpm
mysql-boost-5.7.31.tar.gz  oniguruma-devel-6.8.2-1.el7.x86_64.rpm
nginx-1.19.4               php-7.4.12
nginx-1.19.4.tar.gz        php-7.4.12.tar.bz2
nginx-1.20.1               phpMyAdmin-5.0.2-all-languages.zip
nginx-1.20.1.tar.gz
[root@server1 ~]# cd nginx-1.20.1/
[root@server1 nginx-1.20.1]# vim auto/cc/gcc 
171 # debug
172 #CFLAGS="$CFLAGS -g"
173 
174 # DragonFly's gcc3 generates DWARF
175 #CFLAGS="$CFLAGS -g -gstabs"
[root@server1 nginx-1.20.1]# ls
auto  CHANGES  CHANGES.ru  conf  configure  contrib  html  LICENSE  man  README  src
[root@server1 nginx-1.20.1]# vim auto/cc/gcc
[root@server1 nginx-1.20.1]# ./configure
[root@server1 nginx-1.20.1]# make
[root@server1 nginx-1.20.1]# cd objs/
[root@server1 objs]# \cp -f nginx /opt/nginx/sbin/
[root@server1 objs]# /opt/nginx/sbin/nginx -v

在这里插入图片描述

三、nginx限流

1.控制单ip并发连接数

[root@server1 ~]# cd /usr/local/nginx/conf/ 
[root@server1 conf]# vim nginx.conf 
	location /download/ {
                limit_conn addr 1; #限制并发数
                limit_req zone=one burst=5;
        }
[root@server1 conf]# cd .. 
[root@server1 nginx]# cd html/ 
[root@server1 html]# mkdir download 
[root@server1 html]# cd download/
[root@server1 download]# ls vim.jpg 
[root@server1 conf]# nginx -t 
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok 
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful 
[root@server1 download]# nginx -s reload 
[root@foundation15 ~]# ab -c 10 -n 10 http://172.25.15.1/download/vim.jpg  ##压力测试,-c指定并发数,-n指定请求数 

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

四、nginx配置管理

1.缓存配置

[root@server1 conf]# vim nginx.conf 
	location ~ .*\.(gif|jpg|png)$ {
			 expires 365d; ##缓存有效时长 
			 root html;
    }
[root@server1 conf]# nginx -s reload 
[root@foundation15 ~]# curl -I http://172.25.15.1/download/vim.jpg

在这里插入图片描述
在这里插入图片描述

2.日志轮询

[root@server1 logs]# cd /opt/
[root@server1 opt]# ls
[root@server1 opt]# vim nginx_log.sh
#!/bin/bash
cd /usr/local/nginx/logs && mv access.log access_$(date +%F -d -1day).log  ##日志文件重命名
kill -USR1 `cat /usr/local/nginx/logs/nginx.pid`  ##重载日志
[root@server1 opt]# chmod +x nginx_log.sh 
[root@server1 opt]# ./nginx_log.sh
[root@server1 opt]# cd /usr/local/nginx/logs/
[root@server1 logs]# ll
total 164
-rw-r--r-- 1 root  root 87260 Apr  7 19:04 access_2021-04-06.log  ##生成新的日志文件
-rw-r--r-- 1 nginx root     0 Apr  7 19:10 access.log
-rw-r--r-- 1 nginx root 72817 Apr  7 19:04 error.log
-rw-r--r-- 1 root  root     5 Apr  7 14:15 nginx.pid

3.目录和文件的限制

[root@server1 conf]# vim nginx.conf
            deny 172.25.15.1;
[root@server1 conf]# nginx -s reload
[root@foundation15 ~]# wget http://172.25.15.1/download/vim.jpg

4.中文乱码

[root@server1 nginx]# cd html/ 
[root@server1 html]# vim index.html 
[root@server1 conf]# vim nginx.conf 
	charset utf-8; 
[root@server1 conf]# nginx -s reload

在这里插入图片描述

5.限制ip

[root@server1 conf]# vim nginx.conf
 62             deny all; 
[root@server1 conf]# nginx -s reload

在这里插入图片描述

五、nginx重定向

1. 防止域名恶意解析到服务器IP

vim /usr/local/nginx/conf/nginx.conf
	server { 
		listen 80; 
		server_name   _; 
		return 500; 
	} 

在这里插入图片描述

2.也可以重定向

vim /usr/local/nginx/conf/nginx.conf

	server { 
		listen 80; 
		server_name   _; 
		rewrite ^(.*) http://www.westos.org permanent; 
	} 

3.80重定向443

vim /usr/local/nginx/conf/nginx.conf
	server {
		listen 80;
		server_name www.westos.org;
		rewrite ^/(.*)$ https://www.westos.org/$1 permanent;
	}

4.www.westos.org/bbs 重定向bbs.westos.org

vim /usr/local/nginx/conf/nginx.conf
	rewrite ^/bbs$ http://bbs.westos.org permanent;
	rewrite ^/bbs/(.*)$ http://bbs.westos.org/$1 permanent;

在这里插入图片描述

5.bbs.westos.org 重定向www.westos.org/bbs

vim /usr/local/nginx/conf/nginx.conf
	if ($host = "bbs.westos.org"){
		rewrite ^/(.*)$ http://www.westos.org/bbs/$1 permanent;
	}

在这里插入图片描述

六、nginx防盗链

vim /usr/local/nginx/conf/nginx.conf
配置模块防盗链模块
	location ~ \.(jpg|png)$ {
                valid_referers none blocked www.westos.org;
                if ($invalid_referer) {
                        return 403;
                        #rewrite ^/ http://www2.westos.org/daolian.jpg;
                }
        }

在这里插入图片描述

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值