天天看點

2.lamp-----nginx常見配置一、并發優化二、nginx平滑更新三、nginx限流四、nginx配置管理五、nginx重定向六、nginx防盜鍊

nginx常見配置

  • 一、并發優化
    • 将nginx複制到server3
      • server3操作
      • server2操作
      • 真機檢視
  • 二、nginx平滑更新
    • 1.安裝低版本
    • 2.解壓新版本隻做make不make install
  • 三、nginx限流
    • 1.控制單ip并發連接配接數
  • 四、nginx配置管理
    • 1.緩存配置
    • 2.日志輪詢
    • 3.目錄和檔案的限制
    • 4.中文亂碼
    • 5.限制ip
  • 五、nginx重定向
    • 1. 防止域名惡意解析到伺服器IP
    • 2.也可以重定向
    • 3.80重定向443
    • 4.www.westos.org/bbs 重定向bbs.westos.org
    • 5.bbs.westos.org 重定向www.westos.org/bbs
  • 六、nginx防盜鍊

一、并發優化

将nginx複制到server3

[[email protected] ~]# cd nginx-1.20.1/
[[email protected] nginx-1.20.1]# ls
auto     CHANGES.ru  configure  html     Makefile  objs    src
CHANGES  conf        contrib    LICENSE  man       README
[[email protected] nginx-1.20.1]# cd /usr/local
[[email protected] local]# ls
bin  etc  games  include  lib  lib64  libexec  nginx  php  sbin  share  src
[roo[email protected] local]# scp -r nginx/ server3:/usr/local		#複制到server3
           
2.lamp-----nginx常見配置一、并發優化二、nginx平滑更新三、nginx限流四、nginx配置管理五、nginx重定向六、nginx防盜鍊

server3操作

[[email protected] ~]# cd /usr/local
[[email protected] local]# ls
bin  games    lib    libexec  sbin   src
etc  include  lib64  nginx    share
[[email protected] local]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/	#配置快速啟動,環境變量問題配置完成,可直接在指令行啟動nginx

[[email protected] ~]# cd /usr/local/nginx/
[[email protected] nginx]# ls
client_body_temp  fastcgi_temp  logs        sbin       uwsgi_temp
conf              html          proxy_temp  scgi_temp
[[email protected] nginx]# cd conf/
[[email protected] 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
[[email protected] nginx]# useradd nginx	#建立使用者nginx
[[email protected] 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
[[email protected] nginx]# nginx	#啟動nginx
[[email protected] nginx]# nginx -s reload
[[email protected] nginx]# cd conf
[[email protected] 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
[[email protected] conf]# vim nginx.conf	#修改預設釋出 
           
2.lamp-----nginx常見配置一、并發優化二、nginx平滑更新三、nginx限流四、nginx配置管理五、nginx重定向六、nginx防盜鍊
2.lamp-----nginx常見配置一、并發優化二、nginx平滑更新三、nginx限流四、nginx配置管理五、nginx重定向六、nginx防盜鍊
2.lamp-----nginx常見配置一、并發優化二、nginx平滑更新三、nginx限流四、nginx配置管理五、nginx重定向六、nginx防盜鍊

server2操作

[[email protected] ~]# yum install httpd	#安裝httpd服務
[[email protected] ~]# systemctl enable  --now http
[[email protected] ~]# firewall-cmd --permanent --add-service=http
success
[[email protected] ~]# firewall-cmd --reload 

[[email protected] ~]# cd /usr/local/nginx/conf/
[[email protected] 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
[[email protected] conf]# vim nginx.conf
[[email protected] 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
[[email protected] conf]# nginx -s reload

[[email protected] ~]# cd /var/www/html/
[[email protected] html]# ls
[[email protected] html]# echo server2 > index.html

[[email protected] ~]# cd /usr/local/nginx/html/
[[email protected] html]# ls
50x.html  index.html  index.php
[[email protected] html]# echo server3 > index.html 
[[email protected] html]# nginx -s reload	#重新啟動ngnix
[[email protected] html]# systemctl  disable --now firewalld	#關閉防火牆
           
2.lamp-----nginx常見配置一、并發優化二、nginx平滑更新三、nginx限流四、nginx配置管理五、nginx重定向六、nginx防盜鍊
2.lamp-----nginx常見配置一、并發優化二、nginx平滑更新三、nginx限流四、nginx配置管理五、nginx重定向六、nginx防盜鍊
2.lamp-----nginx常見配置一、并發優化二、nginx平滑更新三、nginx限流四、nginx配置管理五、nginx重定向六、nginx防盜鍊
2.lamp-----nginx常見配置一、并發優化二、nginx平滑更新三、nginx限流四、nginx配置管理五、nginx重定向六、nginx防盜鍊

真機檢視

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

[[email protected] ~]# curl www.westos.org
server2
[[email protected] ~]# curl www.westos.org
server3
           
2.lamp-----nginx常見配置一、并發優化二、nginx平滑更新三、nginx限流四、nginx配置管理五、nginx重定向六、nginx防盜鍊

二、nginx平滑更新

1.安裝低版本

[[email protected] ~]# ls
nginx-1.19.4.tar.gz               
nginx-1.20.1.tar.gz      
[[email protected] ~]# tar zxf nginx-1.19.4.tar.gz 
[[email protected] ~]# cd nginx-1.19.4/
[[email protected] nginx-1.19.4]# ls
auto     CHANGES.ru  configure  html     man     src
CHANGES  conf        contrib    LICENSE  README
[[email protected] 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"
[[email protected] nginx-1.19.4]# ./configure --prefix=/opt/nginx	#編譯源碼
[[email protected] nginx-1.19.4]# make 
[[email protected] nginx-1.19.4]# make install	#安裝

備份原檔案
[[email protected] nginx-1.19.4]# cd /opt/nginx/sbin/
[[email protected] sbin]# ls
nginx
[[email protected] sbin]# cp nginx nginx.old
[[email protected] sbin]# ls
nginx  nginx.old
           
2.lamp-----nginx常見配置一、并發優化二、nginx平滑更新三、nginx限流四、nginx配置管理五、nginx重定向六、nginx防盜鍊
2.lamp-----nginx常見配置一、并發優化二、nginx平滑更新三、nginx限流四、nginx配置管理五、nginx重定向六、nginx防盜鍊
2.lamp-----nginx常見配置一、并發優化二、nginx平滑更新三、nginx限流四、nginx配置管理五、nginx重定向六、nginx防盜鍊

2.解壓新版本隻做make不make install

[[email protected] ~]# tar zxf nginx-1.20.1.tar.gz
[[email protected] ~]# 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
[[email protected] ~]# cd nginx-1.20.1/
[[email protected] 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"
[[email protected] nginx-1.20.1]# ls
auto  CHANGES  CHANGES.ru  conf  configure  contrib  html  LICENSE  man  README  src
[[email protected] nginx-1.20.1]# vim auto/cc/gcc
[[email protected] nginx-1.20.1]# ./configure
[[email protected] nginx-1.20.1]# make
[[email protected] nginx-1.20.1]# cd objs/
[[email protected] objs]# \cp -f nginx /opt/nginx/sbin/
[[email protected] objs]# /opt/nginx/sbin/nginx -v
           
2.lamp-----nginx常見配置一、并發優化二、nginx平滑更新三、nginx限流四、nginx配置管理五、nginx重定向六、nginx防盜鍊

三、nginx限流

1.控制單ip并發連接配接數

[[email protected] ~]# cd /usr/local/nginx/conf/ 
[[email protected] conf]# vim nginx.conf 
	location /download/ {
                limit_conn addr 1; #限制并發數
                limit_req zone=one burst=5;
        }
[[email protected] conf]# cd .. 
[[email protected] nginx]# cd html/ 
[[email protected] html]# mkdir download 
[[email protected] html]# cd download/
[[email protected] download]# ls vim.jpg 
[[email protected] 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 
[[email protected] download]# nginx -s reload 
[[email protected] ~]# ab -c 10 -n 10 http://172.25.15.1/download/vim.jpg  ##壓力測試,-c指定并發數,-n指定請求數 
           
2.lamp-----nginx常見配置一、并發優化二、nginx平滑更新三、nginx限流四、nginx配置管理五、nginx重定向六、nginx防盜鍊
2.lamp-----nginx常見配置一、并發優化二、nginx平滑更新三、nginx限流四、nginx配置管理五、nginx重定向六、nginx防盜鍊
2.lamp-----nginx常見配置一、并發優化二、nginx平滑更新三、nginx限流四、nginx配置管理五、nginx重定向六、nginx防盜鍊

四、nginx配置管理

1.緩存配置

[[email protected] conf]# vim nginx.conf 
	location ~ .*\.(gif|jpg|png)$ {
			 expires 365d; ##緩存有效時長 
			 root html;
    }
[[email protected] conf]# nginx -s reload 
[roo[email protected] ~]# curl -I http://172.25.15.1/download/vim.jpg

           
2.lamp-----nginx常見配置一、并發優化二、nginx平滑更新三、nginx限流四、nginx配置管理五、nginx重定向六、nginx防盜鍊
2.lamp-----nginx常見配置一、并發優化二、nginx平滑更新三、nginx限流四、nginx配置管理五、nginx重定向六、nginx防盜鍊

2.日志輪詢

[[email protected] logs]# cd /opt/
[[email protected] opt]# ls
[[email protected] 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`  ##重載日志
[[email protected] opt]# chmod +x nginx_log.sh 
[[email protected] opt]# ./nginx_log.sh
[[email protected] opt]# cd /usr/local/nginx/logs/
[[email protected] 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.目錄和檔案的限制

[[email protected] conf]# vim nginx.conf
            deny 172.25.15.1;
[[email protected] conf]# nginx -s reload
[[email protected] ~]# wget http://172.25.15.1/download/vim.jpg
           

4.中文亂碼

[[email protected] nginx]# cd html/ 
[[email protected] html]# vim index.html 
[[email protected] conf]# vim nginx.conf 
	charset utf-8; 
[[email protected] conf]# nginx -s reload
           
2.lamp-----nginx常見配置一、并發優化二、nginx平滑更新三、nginx限流四、nginx配置管理五、nginx重定向六、nginx防盜鍊

5.限制ip

[[email protected] conf]# vim nginx.conf
 62             deny all; 
[[email protected] conf]# nginx -s reload
           
2.lamp-----nginx常見配置一、并發優化二、nginx平滑更新三、nginx限流四、nginx配置管理五、nginx重定向六、nginx防盜鍊

五、nginx重定向

1. 防止域名惡意解析到伺服器IP

vim /usr/local/nginx/conf/nginx.conf
	server { 
		listen 80; 
		server_name   _; 
		return 500; 
	} 
           
2.lamp-----nginx常見配置一、并發優化二、nginx平滑更新三、nginx限流四、nginx配置管理五、nginx重定向六、nginx防盜鍊

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;
	}
           
2.lamp-----nginx常見配置一、并發優化二、nginx平滑更新三、nginx限流四、nginx配置管理五、nginx重定向六、nginx防盜鍊

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;
           
2.lamp-----nginx常見配置一、并發優化二、nginx平滑更新三、nginx限流四、nginx配置管理五、nginx重定向六、nginx防盜鍊

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;
	}
           
2.lamp-----nginx常見配置一、并發優化二、nginx平滑更新三、nginx限流四、nginx配置管理五、nginx重定向六、nginx防盜鍊

六、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.lamp-----nginx常見配置一、并發優化二、nginx平滑更新三、nginx限流四、nginx配置管理五、nginx重定向六、nginx防盜鍊

繼續閱讀