【Web】Nginx配置規則


Nginx配置基本說明

   以下是nginx的基本配置文件如下(編輯命令:vi /usr/local/nginx/conf/nginx.conf):

  1 #user  nobody;
  2 #nginx進程數,建議設置為等於CPU總核心數。
  3 worker_processes  1;  
  4 
  5 #全局錯誤日志定義類型,[ debug | info | notice | warn | error | crit ]
  6 #error_log  logs/error.log;
  7 #error_log  logs/error.log  notice;
  8 #error_log  logs/error.log  info;
  9 
 10 #進程pid文件
 11 #pid        logs/nginx.pid;
 12 
 13 
 14 events {
 15     #單個進程最大連接數(最大連接數=連接數*進程數),一個請求的連接一般是2(靜態)和4(代理)
 16     worker_connections  1024;
 17 }
 18 
 19 #設定http服務器,利用它的反向代理功能提供負載均衡支持
 20 http {
 21     #文件擴展名與文件類型映射表
 22     include       mime.types;
 23     #默認文件類型
 24     default_type  application/octet-stream;
 25 
 26     #日志格式設定
 27     #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
 28     #                  '$status $body_bytes_sent "$http_referer" '
 29     #                  '"$http_user_agent" "$http_x_forwarded_for"';
 30     
 31     #access_log  logs/access.log  main;
 32 
 33     #開啟高效文件傳輸模式
 34     sendfile        on;
 35     #此選項允許或禁止使用socke的TCP_CORK的選項,此選項僅在使用sendfile的時候使用
 36     #tcp_nopush     on;
 37 
 38     #長連接超時時間,單位是秒
 39     #keepalive_timeout  0;
 40     keepalive_timeout  65;
 41 
 42     #gzip模塊設置
 43     #gzip  on;
 44 
 45     #虛擬主機的配置
 46     server {
 47         #監聽端口
 48         listen       80;
 49         #域名可以有多個,用空格隔開
 50         server_name  localhost;
 51 
 52         #charset koi8-r;
 53 
 54         #定義本虛擬主機的訪問日志
 55         #access_log  logs/host.access.log  main;
 56         #代理位置
 57         location / {
 58             root   html; //根目錄
 59             index  index.html index.htm; //首頁
 60 
 61         }
 62     
 63         #錯誤頁
 64         #error_page  404              /404.html;
 65         #重定向錯誤頁
 66         # redirect server error pages to the static page /50x.html
 67         #
 68         error_page   500 502 503 504  /50x.html;
 69         location = /50x.html {
 70             root   html;
 71         }
 72 
 73 
 74 
 75         # proxy the PHP scripts to Apache listening on 127.0.0.1:80
 76         #
 77         #location ~ \.php$ {
 78         #    proxy_pass   http://127.0.0.1;
 79         #}
 80 
 81         # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
 82         #
 83         #location ~ \.php$ {
 84         #    root           html;
 85         #    fastcgi_pass   127.0.0.1:9000;
 86         #    fastcgi_index  index.php;
 87         #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
 88         #    include        fastcgi_params;
 89         #}
 90 
 91         # deny access to .htaccess files, if Apache's document root
 92         # concurs with nginx's one
 93         #
 94         #location ~ /\.ht {
 95         #    deny  all;
 96         #}
 97     }
 98 
 99 
100     # another virtual host using mix of IP-, name-, and port-based configuration
101     #
102     #server {
103     #    listen       8000;
104     #    listen       somename:8080;
105     #    server_name  somename  alias  another.alias;
106 
107     #    location / {
108     #        root   html;
109     #        index  index.html index.htm;
110     #    }
111     #}
112 
113 
114     # HTTPS server
115     #
116     #server {
117     #    listen       443 ssl;
118     #    server_name  localhost;
119 
120     #    ssl_certificate      cert.pem;
121     #    ssl_certificate_key  cert.key;
122 
123     #    ssl_session_cache    shared:SSL:1m;
124     #    ssl_session_timeout  5m;
125 
126     #    ssl_ciphers  HIGH:!aNULL:!MD5;
127     #    ssl_prefer_server_ciphers  on;
128 
129     #    location / {
130     #        root   html;
131     #        index  index.html index.htm;
132     #    }
133     #}
134 
135 }
136 
137     

   檢查配置文件是否正確命令:/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
  

Location配置規則

  location

  語法:location [=|~|~*|^~] /uri/{...}

  使用范圍:server中使用

  這個參數根據URI的不同需求進行配置,可以使用字符串與正則表達式匹配,如果要使用正則表達式,你必須制定下列前綴:

  • ~:區分大小寫
  • ~*:不區分大小寫
  • ^*:禁止表達式匹配
  • =:精確匹配

  例子如下:

 1 location = / {
 2     #只匹配/的查詢
 3     [configuration A]
 4 }
 5 
 6 location / {
 7     #匹配任何以/開始的查詢,但是正則表達式與一些長的字符串將被首先匹配
 8     [configuration B]
 9 }
10 
11 location ^- /images/ {
12     #匹配任何以/images/開始的查詢並且停止搜索,不檢查正則表達式
13     [configuration C]
14 }
15 
16 location ~* \.(gif|jpg|png)$ {
17     #匹配任何以gif|jpg|png結尾的文件,但是所有/images/目錄的請求在configuration C處理
18     [configuration D]
19 }
20 
21 各請求的計算如下:
22 ./ -> configuration A
23 ./documents/document.html -> configuration B
24 ./images/1.gif -> configuration C
25 ./documents/1.jpg -> configuration D
View Code

 

其他功能配置

配置訪問日志及錯誤日歷

  • 去掉配置文件前端的日志文件格式注釋
  • 在server模塊中,設置訪問日志地址,以及錯誤日志地址
  • 重啟nginx,並訪問server即可看到相應的日志文件中有日志。

配置錯誤頁面

  • 在server模塊中,設置error_page,並且可以指定錯誤頁面的根目錄
  • 重啟nginx,並訪問server報錯,即可看到制定的錯誤界面。

配置自動索引及別名功能

  • 在server模塊中,加入一個location,開啟自動索引,然后在更目錄下新建文件data和其他文件,如下:

    在瀏覽器中輸入http://server/data,進行訪問,如下:


  • 在server模塊中,進入一個location,開啟別名功能,如下:

    在瀏覽器中輸入http://server/b,進行訪問,請求實際訪問的地址是/usr/local/nginx/html2。

配置文件瀏覽器緩存

  • 設置圖片緩存時間為1天
      

配置下載限速

  • nginx可對下載文件進行限制,在location中加入參數:

      

配置訪問控制及身份驗證

  • 在location中加入參數如下:
  • 配置密碼文件,在/usr/local/nginx目錄下新建文件.htpasswd文件,並且使用命令添加用戶名和密碼,命令:printf "test:$(openssl passwd -crypt 123456)\n" >>/usr/local/nginx/.htpasswd

    其中用戶名是:test,密碼是:123456
  • 在瀏覽器中進行訪問,需要密碼進行登陸。如下

 

 

配置htts代理

  • 需要nginx支持ssl模塊
    即編譯nginx加入此模塊(--with-http_ssl_module),命令:./configure --prefix=/data/soft/nginx --with-http_ssl_module
  • 配置文件配置如下
    1 location /https {
    2                 proxy_http_version 1.1;
    3                 proxy_pass          https://www.baidu.com;
    4                 proxy_set_header    X-B3-TraceId  $request_id;
    5                 proxy_set_header    X-Span-Name    $uri;
    6                 proxy_set_header    Host    www.baidu.com;
    7                 proxy_set_header    X-Real-IP   $remote_addr;
    8                 proxy_set_header    X-Forwarded-For $proxy_add_x_forwarded_for;
    9         }

     

  • 重啟nginx,即可代理https的請求

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM