1. 程式人生 > 其它 >LNMP介紹、部署(nginx調優)(rewrite重寫)04

LNMP介紹、部署(nginx調優)(rewrite重寫)04

LNMP介紹、部署(nginx調優)(rewrite重寫)

1、LNMP環境的搭建部署

2、Rewrite重寫

3、Nginx的調優

安裝軟體包、建立使用者、解壓:

wget http://nginx.org/download/nginx-1.14.0.tar.gz

yum -y install gcc pcre pcre-devl zlib zlib-devel openssl openssl-devel

groupadd www

useradd -g www wwww

useradd -g www www

tar xf nginx-1.14.0.tar.gz

cd nginx-1.14.0/

編譯安裝:

./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-threads

make && make install

yum -y install mariadb mariadb-server mariadb-devel php php-mysql php-fpm

重啟相關服務:

systemctl restart mariadb

systemctl status mariadb

systemctl enable mariadb

systemctl enable php-fpm

systemctl restart php-fpm

systemctl status php-fpm

/usr/local/nginx/sbin/nginx

ps -ef | grep nging

LNMP架構:

Nginx連線php

[root@localhost ~]# cd /usr/local/nginx/conf/

[root@localhost conf]# vim nginx.conf

<?php

phpinfo();

?>

測試訪問:

連線資料庫:

<?php

$link=mysql_connect('localhost','root','123456');

if(!$link) echo '<p>failed.</p>';

else echo '<p>successed.</p>';

mysql_close();

?>

測試訪問:

Nginx地址重寫:

[root@localhost html]# pwd

/usr/local/nginx/html

域名跳轉:(跳轉百度)

檢查語法,重新載入,訪問測試

[root@localhost html]# /usr/local/nginx/sbin/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@localhost html]# /usr/local/nginx/sbin/nginx -s reload

域名跳轉(續1):

[root@localhost html]# vim /usr/local/nginx/conf/nginx.conf

[root@localhost html]# /usr/local/nginx/sbin/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@localhost html]# /usr/local/nginx/sbin/nginx -s reload

測試:

實驗說明:在開啟兩臺機器

Web01web02都要操作:

yum install lrzsz httpd -y

cd /var/www/

ls

cd html/

ls

vim index.html

a.html b.html (兩臺機器分別)

systemctl restart httpd

history

網頁測試訪問:

Nginx:(實現負載均衡)

[root@localhost html]# /usr/local/nginx/sbin/nginx -t

[root@localhost html]# /usr/local/nginx/sbin/nginx -s reload

叢集屬性(輪詢、排程演算法)

測試

測試

測試

排程nginx ssh排程

./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-threads --with-stream

events {

worker_connections 1024;

}

stream {

upstream sshserver {

server 192.168.31.113;

}

server {

listen 800;

proxy_pass sshserver;

}

}

Nginx併發優化:

ab -n 10000 -c 10000 http://localhost/index.html

#!/bin/bash

URL=http://192.168.31.113/index.html?

for i in {1..5000}

do

URL=${URL}x$i

done

curl $URL

優化:

client_header_buffer_size 512k;

large_client_header_buffers 4 512k;

client_header_buffer_size 512k; //該引數對發自客戶端的http頭資訊的大小進行了限制,這個值和large_client_header_buffers同時限制了http請求頭的大小,超過其中一個值則伺服器會返回錯誤狀態碼 414(Request-URI Too Large)。該引數的預設值為1K

large_client_header_buffers 7 512k; //該引數對nginx伺服器接受客戶端請求的頭資訊時所分配的最大緩衝區的大小做了限制,也就是nginx伺服器一次接受一個客戶端請求可就收的最大都資訊大小。這個頭不僅包含 request-line,還包括通用資訊頭、請求頭域、響應頭域的長度總和。這也相當程度的限制了url的長度。nginx伺服器預設的限制是4K或者8K,這是根據伺服器的硬體配置有關的,一般為記憶體一頁的大小,目前大部分為4K,即4096位元組。

location ~.*\.(jpg|png|jif)$ {

expires 30d;

}

手動編寫404錯誤

測試:

Vim 404.html

過載

檢視伺服器狀態資訊

location /status {

stub_status on;

}

location /status {

stub_status on;

allow 192.168.31.113;

deny all;

}

案例:

gzip on;

gzip_min_length 1k;

gzip_comp_level 3;

gzip_type text/vml image/png text/plain application/json a

pplication/msword application/pdf;