亚洲视频二区_亚洲欧洲日本天天堂在线观看_日韩一区二区在线观看_中文字幕不卡一区

公告:魔扣目錄網為廣大站長提供免費收錄網站服務,提交前請做好本站友鏈:【 網站目錄:http://www.430618.com 】, 免友鏈快審服務(50元/站),

點擊這里在線咨詢客服
新站提交
  • 網站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會員:747

 

1 Nginx簡介

Nginx是一個輕量級的高性能HTTP反向代理服務器,同時它也是一個通用類型的代理服務器,支持絕大部分協議,如TCP、UDP、SMTP、HTTPS等。

用Nginx代理后,客戶端的請求由其進行分發到服務器處理,服務器處理完后再返回Nginx,由Nginx結果返回給客戶端。

 

2 linux中Nginx安裝

服務器創建Nginx目錄并進入:

[root@localhost]# mkdir /soft && mkdir /soft/nginx/  
[root@localhost]# cd /soft/nginx/  

?下載Nginx安裝包

可以服務器遠程工具上傳已經下載好的壓縮包,也用wget命令服務器在線下載壓縮包:

[root@localhost]# wget https://nginx.org/download/nginx-1.21.6.tar.gz  

wget命令的可通過yum命令安裝;

?命令解壓Nginx壓縮包:

[root@localhost]# tar -xvzf nginx-1.21.6.tar.gz  

?下載并安裝Nginx所需的依賴庫和包:

[root@localhost]# yum install --downloadonly --downloaddir=/soft/nginx/ gcc-c++  
[root@localhost]# yum install --downloadonly --downloaddir=/soft/nginx/ pcre pcre-devel4  
[root@localhost]# yum install --downloadonly --downloaddir=/soft/nginx/ zlib zlib-devel  
[root@localhost]# yum install --downloadonly --downloaddir=/soft/nginx/ openssl openssl-devel 

 

完成后ls查看,所有需要依賴都在里面:

 

然后用rpm命令依次構建每個依賴包;或用以下下指令一鍵安裝全部依賴包:

[root@localhost]# rpm -ivh --nodeps *.rpm  

?cd到nginx目錄,執行Nginx配置腳本,提前配置好環境便于后面安裝,默認位于/usr/local/nginx/目錄:

[root@localhost]# cd nginx-1.21.6  
[root@localhost]# ./configure --prefix=/soft/nginx/  

?執行命令編譯并安裝Nginx:

[root@localhost]# make && make install  

?回到/soft/nginx/目錄,用ls可看到安裝nginx后生成的文件。

?修改安裝后conf目錄下的nginx.conf:

[root@localhost]# vi conf/nginx.conf  
修改端口號:listen    80;  
修改IP地址:server_name  你當前機器的本地IP(線上配置域名);  

?制定Nginx配置文件并啟動:

[root@localhost]# sbin/nginx -c conf/nginx.conf  
[root@localhost]# ps aux | grep nginx  

 

?放開80端口,刷新服務器防火墻:

[root@localhost]# firewall-cmd --zone=public --add-port=80/tcp --permanent  
[root@localhost]# firewall-cmd --reload  
[root@localhost]# firewall-cmd --zone=public --list-ports  

?瀏覽器輸入Nginx配的IP或域名訪問,如果你看到了Nginx歡迎界面,那么恭喜你安裝成功。

 

3 Nginx配置詳解及優化

1、Nginx用戶及組。

user nginx nginx ;

2、工作進程數量,按實際生產機器調整,通常等于CPU數量或者2倍于CPU。

worker_processes 8;

3、錯誤日志存放路徑配置。

error_log logs/error.log; 
error_log logs/error.log notice; 
error_log logs/error.log info; 

4、pid的存放路徑。

pid logs/nginx.pid;

5、指定進程可以打開的數目。

worker_rlimit_nofile 204800;

6、每個進程的最大連接數,實際生產中按照機器配置調整,原則上sh設置大一點,但不超過CPU100%。

理論上單臺nginx允許的最大連接數為:worker_processes*worker_connections

worker_connections 204800;

7、keepalive超時時間。

keepalive_timeout 60;

8、客戶端請求頭部的緩沖區大小。該參數根據實際業務需要來設置(例如分頁)。

client_header_buffer_size 4k;

9、打開的文件指定緩存,默認不啟用;

max為緩存數量(建議和打開文件數量一樣);
inactive為文件多久沒有請求刪除該文件緩存。
open_file_cache max=65535 inactive=60s;

10、緩存有效信息檢測時間。

open_file_cache_valid 80s;

11、open_file_cache命令中的inactive參數時間內,設置文件的最少使用次數,超過設置的該數字,文件描述符會在緩存中一直打開。

open_file_cache_min_uses 1;

12、設置http服務器,利用它的反向代理功能提供負載均衡支持

http
{
	include mime.types;
}

mime類型設置,以及類型由mime.type文件定義

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"';
log_format log404 '$status [$time_local] $remote_addr $host$request_uri $sent_http_location';

日志格式設置

$remote_addr與$http_x_forwarded_for用以記錄客戶端的ip地址;
$remote_user:用來記錄客戶端用戶名稱;
$time_local: 用來記錄訪問時間與時區;
$request: 用來記錄請求的url與http協議;
$status: 用來記錄請求狀態;成功是200,
$body_bytes_sent :記錄發送給客戶端文件主體內容大小;
$http_referer:用來記錄從那個頁面鏈接訪問過來的;
$http_user_agent:記錄客戶瀏覽器的相關信息;

log_format命令設置log格式后,需用access_log命令設置log存放路徑;

access_log logs/host.access.log main;
access_log logs/host.access.404.log log404;

13、
server_names_hash_max_size 和server_names_hash_bucket_size用來控制服務器名字hash表的保存

server_names_hash_bucket_size 128;

hash bucket size=hash表大小,且是一路處理器緩存大小的倍數。

如果hash bucket size等于一路處理器緩存的大小,那么在查找鍵的時候,最壞的情況下在內存中查找的次數為2。

14、如果要增大13點中兩個參數的提示,首先要增大client_header_buffer_size參數值

client_header_buffer_size 4k;

15、客戶端請求頭部的緩沖區大小。分頁大小可以用命令getconf PAGESIZE取得。

large_client_header_buffers 8 128k;

nginx默認會用client_header_buffer_size這個buffer來讀header值,當header過大,則用


large_client_header_buffers來讀header值。

16、設置通過nginx上傳文件的大小

open_file_cache max=102400 inactive=20s;

17、sendfile指令指定 nginx 是否調用sendfile 函數(zero copy 方式)來輸出文件,對于普通應用,必須設為on。如果用來進行下載等應用磁盤IO重負載應用,可設置為off,以平衡磁盤與網絡IO處理速度,降低系統uptime。

sendfile on;

18、允許或禁止socket的TCP_CORK的選項,只有使用了sendfile時才用

tcp_nopush on;

19、后端服務器連接的超時時間

proxy_connect_timeout 90; 

20、后端服務器處理請求的時間

proxy_read_timeout 180;

21、后端服務器數據響應時間

proxy_send_timeout 180;

22、設置從被代理服務器讀取的第一部分應答的緩沖區大小

proxy_buffer_size 256k;

23、設置用于讀取應答的緩沖區數目和大小,默認情況也為分頁大小。

proxy_buffers 4 256k;

24、設置在寫入proxy_temp_path時數據的大小,預防一個工作進程在傳遞文件時阻塞太長

proxy_busy_buffers_size 256k;
 
proxy_temp_file_write_size 256k;

25、proxy_temp_path和proxy_cache_path指定的路徑必須在同一分區

proxy_temp_path /data0/proxy_temp_dir;

26、設置內存緩存空間大小為300MB,如果內容2天未被訪問則自動清除緩存,硬盤緩存空間大小為30GB。

proxy_cache_path /data0/proxy_cache_dir levels=1:2 keys_zone=cache_one:300m inactive=2d max_size=30g;

27、keepalive超時時間。

keepalive_timeout 120;

28、nginx的upstream配置

a、輪詢(默認)

全部請求都按時間順序分配到后端服務器。

b、weight(權重)

很好理解,就是指定輪詢概率,權重大小和訪問概率成正比,用于后端服務器性能不均的情況。

例如:

upstream bakend {
server 192.168.110.230 weight=10;
server 192.168.110.235 weight=5;
}

//此時的設置測192.168.110.230的訪問概率是192.168.110.235的兩倍

c、ip_hash

所有請求都按訪問ip的hash結果分配,如此以來每個用戶可以訪問固定的后端服務器。

可以用來解決session共享的問題,但當前環境下大多用redis等解決session共享了。

例如:

upstream bakend {
ip_hash;
server 192.168.110.230:88;
server 192.168.110.235:80;
}

d、fair

fair是第三方提供的不是nginx官方提供的,即按后端服務器的響應時間來分配請求,響應時間短的優先分配。

例如:

upstream backend {
server server1;
server server2;
fair;
}

e、url_hash

url_hash也是第三方提供的不是nginx官方提供的,即按訪問url的hash結果來分配請求,使每個url定向到同一個后端服務器,后端服務器為緩存時比較有效。

例如:

upstream backend {
server squid1:3128;
server squid2:3128;
hash $request_uri;
hash_method crc32;
}

29、nginx請求限流

限制哪些惡意攻擊請求網站的請求。

#限制用戶連接數來預防DOS攻擊
limit_conn_zone $binary_remote_addr zone=perip:10m;
limit_conn_zone $server_name zone=perserver:10m;
#限制同一客戶端ip最大并發連接數
limit_conn perip 2;
#限制同一server最大并發連接數
limit_conn perserver 20;
#限制下載速度,根據自身服務器帶寬配置
limit_rate 300k;

30、nginx負載均衡

upstream ygoapi{
server ip:port fail_timeout=5 max_fails=3;
server ip:port fail_timeout=5 max_fails=3;
ip_hash; #負載均衡策略
}

31、nginx中htpps配置

server {
listen 443;
server_name 域名;
ssl on;
root /xxx/xxx/html; // 前臺文件存放文件夾,一般使用 Nginx 初始化的文件夾,當然也可以自己修改
index index.html;// 上面配置的文件夾里面的index.html
ssl_certificate /路徑/證書名稱.pem;
ssl_certificate_key /路徑/證書名稱.key;
ssl_session_timeout 5m;
# SSL協議配置
ssl_protocols SSLv2 SSLv3 TLSv1.2; #表示使用TLS類型協議
ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP; #表示使用加密套件的類型
ssl_prefer_server_ciphers on;
location / {
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://127.0.0.1:8080;
}
}
}

server {
listen 80;
server_name your-domain.com;// 你的域名
rewrite ^(.*)$ https://$host:443$1 permanent;// 把http的域名請求轉成https且轉發到443端口
}

32、nginx全局配置

worker_processes 1;
pid /var/run/nginx.pid;

events {
worker_connections 2048;
multi_accept on;
use epoll;
}

http {
include /etc/nginx/mime.types;
default_type application/octet-stream;

log_format main '{"@timestamp":"$time_iso8601",'
'"host":"$server_addr",'
'"clientip":"$remote_addr",'
'"size":$body_bytes_sent,'
'"responsetime":$request_time,'
'"upstreamtime":"$upstream_response_time",'
'"upstreamhost":"$upstream_addr",'
'"http_host":"$host",'
'"url":"$uri",'
'"xff":"$http_x_forwarded_for",'
'"referer":"$http_referer",'
'"agent":"$http_user_agent",'
'"status":"$status"}';

sendfile on;
tcp_nopush on;
tcp_nodelay on;

server_names_hash_bucket_size 128;
server_names_hash_max_size 512;
keepalive_timeout 65;
client_header_timeout 15s;
client_body_timeout 15s;
send_timeout 60s;

limit_conn_zone $binary_remote_addr zone=perip:10m;
limit_conn_zone $server_name zone=perserver:10m;
limit_conn perip 2;
limit_conn perserver 20;
limit_rate 300k;

proxy_cache_path /data/nginx-cache levels=1:2 keys_zone=nginx-cache:20m max_size=50g inactive=168h;

client_body_buffer_size 512k;
client_header_buffer_size 4k;
client_max_body_size 512k;
large_client_header_buffers 2 8k;
proxy_connect_timeout 5s;
proxy_send_timeout 120s;
proxy_read_timeout 120s;
proxy_buffer_size 16k;
proxy_buffers 4 64k;
proxy_busy_buffers_size 128k;
proxy_temp_file_write_size 128k;
proxy_next_upstream http_502 http_504 http_404 error timeout invalid_header;

gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_comp_level 4;
gzip_types text/plain text/css application/json application/x-JAVAscript text/xml application/xml application/xml+rss text/JavaScript;
gzip_vary on;
gzip_disable "MSIE [1-6].";

include /etc/nginx/conf.d/*.conf;
}

33、nginx內核參數配置

#如果想把timewait降下了就要把tcp_max_tw_buckets值減小,默認是180000
net.ipv4.tcp_max_tw_buckets = 5000

#開啟重用功能,允許將TIME-WAIT狀態的sockets重新用于新的TCP連接
net.ipv4.tcp_tw_reuse = 1

#系統中最多有多少個TCP套接字不被關聯到任何一個用戶文件句柄上。如果超過這個數字,孤兒連接將即刻被復位并打印出警告信息。這個限制僅僅是為了防止簡單的DoS攻擊
net.ipv4.tcp_max_orphans = 262144

#當keepalive起用的時候,TCP發送keepalive消息的頻度。缺省是2小時。我們可以調短時間跨度
net.ipv4.tcp_keepalive_time = 30

34、server配置

#隱藏版本信息
server_tokens off;
server {
listen 80;
server_name www.ygoclub.com;
charset utf-8;

#重定向HTTP請求到HTTPS
return 301 https://$server_name$request_uri;
}

分享到:
標簽:Nginx
用戶無頭像

網友整理

注冊時間:

網站:5 個   小程序:0 個  文章:12 篇

  • 51998

    網站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會員

趕快注冊賬號,推廣您的網站吧!
最新入駐小程序

數獨大挑戰2018-06-03

數獨一種數學游戲,玩家需要根據9

答題星2018-06-03

您可以通過答題星輕松地創建試卷

全階人生考試2018-06-03

各種考試題,題庫,初中,高中,大學四六

運動步數有氧達人2018-06-03

記錄運動步數,積累氧氣值。還可偷

每日養生app2018-06-03

每日養生,天天健康

體育訓練成績評定2018-06-03

通用課目體育訓練成績評定