003.Nginx配置解析

来源:https://www.cnblogs.com/itzgr/archive/2020/07/08/13266280.html
-Advertisement-
Play Games

一 Nginx配置文件 1.1 Nginx主配置 Nginx主配置文件/etc/nginx/nginx.conf是一個純文本類型的文件,整個配置文件是以區塊的形式組織,通常每一個區塊以一對大括弧{}來表示開始與結束。 提示:若編譯安裝則為編譯時所指定目錄。 Main位於nginx.conf配置文件的 ...


一 Nginx配置文件

1.1 Nginx主配置

Nginx主配置文件/etc/nginx/nginx.conf是一個純文本類型的文件,整個配置文件是以區塊的形式組織,通常每一個區塊以一對大括弧{}來表示開始與結束。 提示:若編譯安裝則為編譯時所指定目錄。
  • Main位於nginx.conf配置文件的最高層;
  • Main層下可以有Event、HTTP層;
  • Http層下麵允許有多個Server層,用於對不同的網站做不同的配置;
  • Server層下麵允許有多個Location,用於對不同的路徑進行不同模塊的配置。

#如下為全局Main配置:
  1 user  nginx;
  2 worker_processes  1;
  3 
  4 error_log  /var/log/nginx/error.log warn;
  5 pid        /var/run/nginx.pid;
#如下為Event配置:
  1 events {
  2     worker_connections  1024;
  3 }
#如下為http配置:
  1 http {
  2     include       /etc/nginx/mime.types;
  3     default_type  application/octet-stream;
  4     log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
  5                       '$status $body_bytes_sent "$http_referer" '
  6                       '"$http_user_agent" "$http_x_forwarded_for"';
  7     access_log  /var/log/nginx/access.log  main;
  8     sendfile        on;
  9     #tcp_nopush     on;
 10     keepalive_timeout  65;
 11     #gzip  on;
 12     include /etc/nginx/conf.d/*.conf;
 13 }
提示:通常Server配置在獨立的/etc/nginx/conf.d/*.conf中,通過引用的方式調用,如下/etc/nginx/conf.d/default.conf:
  1 server {
  2     listen       80;
  3     server_name  localhost;
  4     location / {
  5         root   /usr/share/nginx/html;
  6         index  index.html index.htm;
  7     }
  8     error_page   500 502 503 504  /50x.html;
  9     location = /50x.html {
 10         root   /usr/share/nginx/html;
 11     }
 12 }

1.2 Nginx全局配置

  1 user  nginx;					#進程用戶
  2 worker_processes  1;				#工作進程,配合和CPU個數保持一致
  3 error_log  /var/log/nginx/error.log warn;	        #錯誤日誌路徑及級別
  4 pid        /var/run/nginx.pid;			#Nginx服務啟動的pid

1.3 Nginx events事件配置

  1 events {
  2     worker_connections  1024;			#每個worker進程支持的最大連接數
  3     use epoll;					#內核模型,select、poll、epoll
  4 }

1.4 Nginx公共配置

  1 http {
  2     include       /etc/nginx/mime.types;	#指定在當前文件中包含另一個文件的指令
  3     default_type  application/octet-stream;	#指定預設處理的文件類型可以是二進位
  4 
  5     log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
  6                       '$status $body_bytes_sent "$http_referer" '
  7                       '"$http_user_agent" "$http_x_forwarded_for"';	#日誌格式
  8 
  9     access_log  /var/log/nginx/access.log  main;			#訪問日誌
 10 
 11     sendfile        on;		#優化靜態資源
 12     #tcp_nopush     on;		#nginx不要緩存數據,而是一段一段發送
 13 
 14     keepalive_timeout  65;	        #給客戶端分配連接超時時間,伺服器會在這個時間過後關閉連接。
 15 
 16     #gzip  on;			#壓縮

1.5 Nginx server配置

Nginx必須使用虛擬機配置站點,每個虛擬主機使用一個server。
  1     server {
  2         listen       80;			        #監聽埠,預設80
  3         server_name  localhost;			#提供服務的功能變數名稱或主機名
  4 
  5         #charset koi8-r;
  6 
  7         #access_log  logs/host.access.log  main;
  8 
  9         location / {				#控制網站訪問路徑
 10             root   /usr/share/nginx/html;	        #存放網站的路徑
 11             index  index.html index.htm;	        #預設訪問的首頁
 12         }
 13         #error_page  404              /404.html;	#錯誤頁面
 14 
 15         # redirect server error pages to the static page /50x.html
 16         #
 17         error_page   500 502 503 504  /50x.html;	#定義請求錯誤,指定錯誤代碼
 18         location = /50x.html {			#錯誤代碼重定向到新的location
 19             root   html;
 20         }
 21     # another virtual host using mix of IP-, name-, and port-based configuration
 22     #
 23     #server {					#server段配置
 24     #    listen       8000;
 25     #    listen       somename:8080;
 26     #    server_name  somename  alias  another.alias;
 27 
 28     #    location / {
 29     #        root   html;
 30     #        index  index.html index.htm;
 31     #    }
 32     #}
 33 
 34 
 35     # HTTPS server
 36     #
 37     #server {					#server段配置
 38     #    listen       443 ssl;
 39     #    server_name  localhost;
 40 
 41     #    ssl_certificate      cert.pem;
 42     #    ssl_certificate_key  cert.key;		#SSL證書配置
 43 
 44     #    ssl_session_cache    shared:SSL:1m;
 45     #    ssl_session_timeout  5m;
 46 
 47     #    ssl_ciphers  HIGH:!aNULL:!MD5;
 48     #    ssl_prefer_server_ciphers  on;
 49 
 50     #    location / {
 51     #        root   html;
 52     #        index  index.html index.htm;
 53     #    }
 54     #}
 55 }
提示:index指令中列出多個文件名,NGINX按指定的順序搜索文件並返回它找到的第一個文件。 Nginx更多配置釋義可參考:https://blog.csdn.net/tsummerb/article/details/79248015。

二 Nginx網站配置

2.1 Nginx配置網站

  1 [root@nginx ~]# vi /etc/nginx/conf.d/base.conf
  2 server {
  3     server_name  base.linuxds.com;
  4     location / {
  5         root   /usr/share/nginx/base;
  6         index  index.html;
  7     }
  8 }
  9 
 10 server {
 11     server_name  blog.linuxds.com;
 12     location / {
 13         root    /usr/share/nginx/blog;
 14         index   index.html;
 15     }
 16     location /ok {
 17         alias   /usr/share/nginx/yes;
 18         index   index.html;
 19     }
 20 }
 21 [root@nginx01 ~]# mkdir -p /usr/share/nginx/{base,blog,yes}
 22 [root@nginx01 ~]# echo '<h1>www</h1>' > /usr/share/nginx/base/index.html
 23 [root@nginx01 ~]# echo '<h1>blog</h1>' > /usr/share/nginx/blog/index.html
 24 [root@nginx01 ~]# echo '<h1>love</h1>' > /usr/share/nginx/blog/love.html
 25 [root@nginx01 ~]# echo '<h1>yes</h1>' > /usr/share/nginx/yes/index.html
 26 [root@nginx01 ~]# nginx -t -c /etc/nginx/nginx.conf		#檢查配置文件
 27 [root@nginx01 ~]# nginx -s reload				#重載配置文件

2.2 測試訪問

瀏覽器訪問:base.linuxds.com clipboard 瀏覽器訪問:blog.linuxds.com clipboard 瀏覽器訪問:blog.linuxds.com/ok clipboard 瀏覽器訪問:blog.linuxds.com/love.html clipboard 註:請添加對應的功能變數名稱解析,添加方式取決於不同的IP及網路環境,具體操作略。

2.3 Nginx配置錯誤頁面

  1 [root@nginx01 ~]# vi /etc/nginx/conf.d/base.conf
  2 server {
  3     server_name  base.linuxds.com;
  4     location / {
  5         root   /usr/share/nginx/base;
  6         index  index.html;
  7     }
  8 }
  9 
 10 server {
 11     server_name  blog.linuxds.com;
 12 
 13     error_page  404 403 500 502 503 504  /baseerror.html;	#配置錯誤頁
 14     location /baseerror.html {
 15         root    /usr/share/nginx/html;
 16     }
 17 
 18     location / {
 19         root    /usr/share/nginx/blog;
 20         index   index.html;
 21     }
 22     location /ok {
 23         alias   /usr/share/nginx/yes;
 24         index   index.html;
 25     }
 26 }
 27 [root@nginx01 ~]# echo '<h1>Error</h1>' > /usr/share/nginx/html/baseerror.html
 28 [root@nginx01 ~]# nginx -t -c /etc/nginx/nginx.conf		#檢查配置文件
 29 [root@nginx01 ~]# nginx -s reload				#重載配置文件

2.4 測試Error

瀏覽器訪問任何一個不存在的頁面,如:http://blog.linuxds.com/hhhh clipboard

三 Nginx相關安全策略

3.1 禁止htaccess

  1 location ~/\.ht {
  2     deny all;
  3 }

3.2 禁止多個目錄

  1 location ~ ^/(picture|move)/ {
  2     deny all;
  3     break;
  4 }

3.3 禁止/data開頭的文件

  1 location ~ ^/data {
  2     deny all;
  3 }

3.4 禁止單個目錄

  1 location /imxhy/images/ {
  2     deny all;
  3 }

3.5 特定允許訪問

  1 root /usr/share/nginx/rewrite/;
  2 allow 208.97.167.194;
  3 allow 222.33.1.2;
  4 allow 231.152.49.4;
  5 deny all;
  6 auth_basic "xhy";
  7 auth_basic_user_file htpasswd;

您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 調試腳本所花費的時間常常比編寫代碼還要多。所有編程語言都應該實現的一個特性就是在出現始料未及的情況時,能夠生成跟蹤信息。調試信息可以幫你弄清楚是什麼原因使得程式行為異常。 ...
  • 大家好,我是良許。 我們在平時工作的時候,經常要知道兩個文件之間,以及同個文件不同版本之間有何異同點。在 Windows 下,有 beyond compare 這個好用的工具,而在 Linux 下,也有很多很強大的工具,良許之前也寫過一篇文章介紹: Linux下9種優秀的代碼比對工具推薦 這些比對工 ...
  • [導讀] 前文大致總結了單片機串口的一些值得註意的要點,本文來梳理一下I2C匯流排的一些應用要點。這個題目有點大,對於I2C其實很多地方也沒講清楚,只為了與前文形成系列,如果大家有補充歡迎留言。說了些閑話,進入正題吧。 I2C之前世今生 \(I^2C\)(Inter-Integrated Circui ...
  • [導讀] 單片機開發串口是應用最為廣泛的通信介面,也是最為簡單的通信介面之一,但是其中的一些要點你是否明瞭呢?來看看本人對串口的一些總結,當然這個總結並不能面面俱到,只是將個人認為具有共性以及相對比較重要的點做了些梳理。 啥是串口? 首先這玩意兒分兩種: **通用非同步收發器(UART)**是用於非同步 ...
  • 1. Shell 腳本的第一行時指定腳本解釋器。 #!/bin/bash 或者 #!/bin/sh 2. Shell 腳本的開頭會加版本,版權等信息 #!/bin/bash #Author: iskylite #Blog: http://www.cnblogs.com/iskylite/ #Date ...
  • 虛擬機與容器 很明顯可以看出兩者在操作系統級別上的隔離和進程上的隔離的區別,VM因為隔離級別更高明顯更重。 linux容器主要技術特點: 文件系統隔離:每個容器都有自己的root文件系統 進程隔離:每個容器都運行在自己的進程環境中 網路隔離:容器件的虛擬網路介面和IP地址都是分開的 資源隔離和分組: ...
  • 轉自:https://www.cnblogs.com/yjlch1016/p/8900841.html 前置條件:不使用openjdk,若想使用openjdk。直接在centos下 使用 yum 查看和安裝 # 查看課安裝的java 包 yum list java-1.8* #使用如下命令安裝 op ...
  • 註:本文章只是小白在學習之餘,總結學習內容,並鍛煉自己的總結能力。內容如果有所錯誤,可以在下方留言指出,希望各位大佬見諒。 正文 利用win7的粘貼提示漏洞破解系統密碼 1、在未登錄系統時,連續按5次shift鍵,彈出程式c:\windows\system32\sethc.exe 這代表著,在你未登 ...
一周排行
    -Advertisement-
    Play Games
  • 基於.NET Framework 4.8 開發的深度學習模型部署測試平臺,提供了YOLO框架的主流系列模型,包括YOLOv8~v9,以及其系列下的Det、Seg、Pose、Obb、Cls等應用場景,同時支持圖像與視頻檢測。模型部署引擎使用的是OpenVINO™、TensorRT、ONNX runti... ...
  • 十年沉澱,重啟開發之路 十年前,我沉浸在開發的海洋中,每日與代碼為伍,與演算法共舞。那時的我,滿懷激情,對技術的追求近乎狂熱。然而,隨著歲月的流逝,生活的忙碌逐漸占據了我的大部分時間,讓我無暇顧及技術的沉澱與積累。 十年間,我經歷了職業生涯的起伏和變遷。從初出茅廬的菜鳥到逐漸嶄露頭角的開發者,我見證了 ...
  • C# 是一種簡單、現代、面向對象和類型安全的編程語言。.NET 是由 Microsoft 創建的開發平臺,平臺包含了語言規範、工具、運行,支持開發各種應用,如Web、移動、桌面等。.NET框架有多個實現,如.NET Framework、.NET Core(及後續的.NET 5+版本),以及社區版本M... ...
  • 前言 本文介紹瞭如何使用三菱提供的MX Component插件實現對三菱PLC軟元件數據的讀寫,記錄了使用電腦模擬,模擬PLC,直至完成測試的詳細流程,並重點介紹了在這個過程中的易錯點,供參考。 用到的軟體: 1. PLC開發編程環境GX Works2,GX Works2下載鏈接 https:// ...
  • 前言 整理這個官方翻譯的系列,原因是網上大部分的 tomcat 版本比較舊,此版本為 v11 最新的版本。 開源項目 從零手寫實現 tomcat minicat 別稱【嗅虎】心有猛虎,輕嗅薔薇。 系列文章 web server apache tomcat11-01-官方文檔入門介紹 web serv ...
  • 1、jQuery介紹 jQuery是什麼 jQuery是一個快速、簡潔的JavaScript框架,是繼Prototype之後又一個優秀的JavaScript代碼庫(或JavaScript框架)。jQuery設計的宗旨是“write Less,Do More”,即倡導寫更少的代碼,做更多的事情。它封裝 ...
  • 前言 之前的文章把js引擎(aardio封裝庫) 微軟開源的js引擎(ChakraCore))寫好了,這篇文章整點js代碼來測一下bug。測試網站:https://fanyi.youdao.com/index.html#/ 逆向思路 逆向思路可以看有道翻譯js逆向(MD5加密,AES加密)附完整源碼 ...
  • 引言 現代的操作系統(Windows,Linux,Mac OS)等都可以同時打開多個軟體(任務),這些軟體在我們的感知上是同時運行的,例如我們可以一邊瀏覽網頁,一邊聽音樂。而CPU執行代碼同一時間只能執行一條,但即使我們的電腦是單核CPU也可以同時運行多個任務,如下圖所示,這是因為我們的 CPU 的 ...
  • 掌握使用Python進行文本英文統計的基本方法,並瞭解如何進一步優化和擴展這些方法,以應對更複雜的文本分析任務。 ...
  • 背景 Redis多數據源常見的場景: 分區數據處理:當數據量增長時,單個Redis實例可能無法處理所有的數據。通過使用多個Redis數據源,可以將數據分區存儲在不同的實例中,使得數據處理更加高效。 多租戶應用程式:對於多租戶應用程式,每個租戶可以擁有自己的Redis數據源,以確保數據隔離和安全性。 ...