Nginx使用實踐總結

Nginx可以作爲靜態頁面的 web 服務器,同時還支持 CGI 協議的動態語言,比如 pe rl 、 php等。但是不支持 java 。 Java 程序只能通過與 tomcat 配合完成。 Nginx 專爲性能優化而開發,性能是其最重要的考量 實現上非常注重效率 ,能經受高負載的考驗 有報告表明能支持高達 50,000 個併發連接數。

【1】自我路由

背景:請求格式如/operation/user,但是nginx.conf配置文件總location均是如/user格式攔截。那麼如何在最小修改情況下實現請求攔截呢?

解決方法一:使用正則修改location的攔截規則,如/opertaion/user|/user。

這樣既攔截了請求也保留了原先的配置。但是這種方法需要修改每個location,比較麻煩。

推薦方案:添加自我路由。

攔截/operation/開頭的並再次轉發給自己:

location /operation/ {
   proxy_pass_header Server;
       proxy_headers_hash_max_size 5120;
       proxy_headers_hash_bucket_size 640;
       proxy_set_header Host $http_host;
       proxy_set_header X-Forwarded-For $remote_addr;
       proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
       proxy_set_header X-Real-IP $remote_addr;
       proxy_set_header X-Scheme $scheme;
       proxy_pass  http://127.0.0.1:21100/;
   }

【2】命令行參數

nginx支持一系列命令行參數,提供快捷功能如停止、啓動、測試配置文件等。

① nginx -s [選項]

nginx -s signal — 給master process 發送指令,指令值可以如下:

stop — 快速停止
quit — 優雅退出
reload — 重新讀取新的配置文件,啓動新的worker processes,平滑退出老的worker proesses
reopen — 打開日誌文件

② nginx -t 測試配置文件

[root@localhost ~]# cd /usr/local/nginx/sbin/
[root@localhost 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

③ nginx -T 測試配置文件並輸出

在這裏插入圖片描述

④ nginx -v 查看nginx版本

[root@localhost sbin]# ./nginx -v
nginx version: nginx/1.16.1

⑤ nginx -V 查看nginx版本、編譯版本與配置參數

[root@localhost sbin]# ./nginx -V
nginx version: nginx/1.16.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC)
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --with-http_ssl_module

⑥ nginx -g directives 命令方式進行配置

實例如下:

nginx -g "pid /var/run/nginx.pid; worker_processes `sysctl -n hw.ncpu`;"
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章