02_Nginx基本配置与参数说明 + 辅助命令
Nginx基本配置與參數說明,下面是nginx.conf配置文件
| #運行用戶 #user? nobody; worker_processes? 2; ? #全局錯誤日志及PID文件 #error_log? logs/error.log; #error_log? logs/error.log? notice; #error_log? logs/error.log? info; ? #pid??????? logs/nginx.pid; ? #工作模式及連接數上限 events { ??? #epoll是多路復用IO(I/O Multiplexing)中的一種方式 ??? #僅用于linux2.6以上內核,可以大大提高nginx的性能 ??? #use epoll ? ??? #單個后臺worker process進程的最大并發鏈接數 ??? worker_connections? 1024; ? ??? #并發總數是worker_processes和worker_connections的成績 ??? #即max_clients = worker_process * worker_connections ??? #在設置了反向代理的情況下,max_clients = woker_processes * worker_connections / 4 ??? #為什么上面的方向代理要除以4,應該說是一個經驗值 ??? #根據以上條件,正常情況下的Nginx Server可以應付的最大連接為:4 * 8000 = 32000 ??? #woker_connections值的設置跟物理內存大小有關 ??? #因為并發受IO約束,max_clients的值須小于系統可以打開的最大文件數 ??? #而系統可以打開的最大文件數和內存大小成正比,一般1GB內存的機器上可以打開的文件數大約是10左右 ??? #我們來看看360M內存的VPS可以打開的文件句柄數是多少: ??? #$ cat /proc/sys/fs/file-max ??? #輸出 34336 (我的是287464) ??? #32000 < 34336,即并發連接總數小于系統可以打開的文件句柄總數,這樣就在操作系統承受的范圍之內 ??? # ??? #所以,worker_connections的值需根據worker_processes進程數目和系統可以打開的最大文件 ??? #總進行適當地進行設置 ??? # ??? #使得并發總數小于操作系統可以打開的最大文件數據 ??? #其實質也就是根據主機的物理CPU和內存進行配置 ??? #當然,理論上的并發總數可能會和實際有所偏差,因為主機還有其他的工作進程需要系統資源 ??? #ulimit -SHn 65535 } ? ? http { ??? #設定mime類型,類型由mime.type文件定義 ??? include?????? mime.types; ??? 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"'; ? ??? access_log? logs/access.log? main; ? ??? #sendfile指令指定nginx是否調用sendfile函數(zero copy方式)來輸出文件, ??? #對于普通應用,必須設為on, ??? #如果用來進行下載等應用磁盤IO重負載應用,可以設置為off ??? #以平衡磁盤與網絡I/O處理速度,降低系統的uptime ??? sendfile??????? on; ??? tcp_nopush???? on; ? ??? #連接超時時間 ??? #keepalive_timeout? 0; ??? keepalive_timeout? 65; ? ??? #開啟gzip壓縮 ??? gzip? on; ??? gzip_disable "MSIE [1-6]."; ? ??? #設定請求緩沖 ??? client_header_buffer_size? 128k; ??? large_client_header_buffers 4 128k; ? ??? #設置虛擬機主機配置 ??? server { ??????? listen?????? 80; ??????? #定義使用www.nginx.cn訪問 ??????? #server_name www.nginx.cn; ??????? server_name? localhost; ? ??????? #定義服務器的默認網站根目錄的訪問日志 ??????? #root html; ? ??????? #charset koi8-r; ? ??????? #設定本虛擬主機的訪問日志 ??????? #access_log? logs/host.access.log? main; ? ??????? #在我的電腦上有:[root@localhost some]# pwd example.html ??????? #/data/www/some ??????? # ??????? #如果按照下面的配置,在瀏覽器上輸入:http://192.168.6.26/some/example.html ??????? #這時候瀏覽器中可以看到頁面內容 ??????? location / { ??????????? root?? /data/www; ??????? } ? ??????? #在我的電腦 ??????? #[root@localhost images]# ??????? #[root@localhost images]# pwd ??????? #/data/images ??????? #[root@localhost images]# ls ??????? #test.png ??????? #[root@localhost images]# ??????? # ??????? #在瀏覽器中輸入:http://192.168.6.26/images/test.png,在頁面中可以看到圖片 ???????? location /images/ { ??????????? root /data; ??????? } ? ??????? #error_page? 404????? ????????/404.html; ? ??????? # redirect server error pages to the static page /50x.html ??????? # 定義錯誤提示頁面 ??????? error_page?? 500 502 503 504? /50x.html; ??????? location = /50x.html { ??????????? root?? html; ??????? } ??????? ??????? #靜態文件,nginx自己處理 ??????? location ~ ^/(images|javascript|js|css|flash|media|static)/ { ???????????? #過期30天,靜態文件不怎么更新,過期可以設大一點 ???????????? #如果頻繁更新,則可以設置得小一點 ???????????? expires 30d; ??????? } ? ??????? # proxy the PHP scripts to Apache listening on 127.0.0.1:80 ??????? # ??????? #location ~ \.php$ { ??????? #??? proxy_pass?? http://127.0.0.1; ??????? #} ? ??????? # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 ??????? # ??????? #location ~ \.php$ { ??????? #??? root?????????? html; ??????? #??? fastcgi_pass?? 127.0.0.1:9000; ??????? #??? fastcgi_index? index.php; ??????? #??? fastcgi_param? SCRIPT_FILENAME? /scripts$fastcgi_script_name; ??????? #??? include??????? fastcgi_params; ??????? #} ? ??????? # deny access to .htaccess files, if Apache's document root ??????? # concurs with nginx's one ??????? # ??????? #禁止訪問,htxxx文件 ??????? #location ~ /\.ht { ??????? #??? deny? all; ??????? #} ??? } ? ??? server { ???????? location / { ???????? ????? proxy_pass http://192.168.6.26:8080; ???????? }???? ? ???????? #表示只要是這些歌文件就轉到/data/images ???????? location ~ \.(gif|jpg|png)$ { ????????????? root /data/images; ???????? } ??? } ? ? ??? # another virtual host using mix of IP-, name-, and port-based configuration ?? ?# ??? #server { ??? #??? listen?????? 8000; ??? #??? listen?????? somename:8080; ??? #??? server_name? somename? alias? another.alias; ? ??? #??? location / { ??? #??????? root?? html; ??? #??????? index? index.html index.htm; ??? #??? } ??? #} ? ??? # HTTPS server ??? # ??? #server { ??? #??? listen?????? 443 ssl; ??? #??? server_name? localhost; ? ??? #??? ssl_certificate????? cert.pem; ??? #??? ssl_certificate_key? cert.key; ? ??? #??? ssl_session_cache??? shared:SSL:1m; ??? #??? ssl_session_timeout? 5m; ? ??? #??? ssl_ciphers? HIGH:!aNULL:!MD5; ??? #??? ssl_prefer_server_ciphers? on; ? ??? #??? location / { ??? #??????? root?? html; ??? #??????? index? index.html index.htm; ??? #??? } ??? #} } |
2 輔助知識點:
#總核數 = 物理CPU個數 * 每顆物理CPU的核數
#總邏輯CPU數 = 物理CPU個數 * 每顆物理CPU的核數 * 超線程數
?
#查看物理CPU個數(以下是我虛擬機中的信息):
[root@localhost ~]# cat /proc/cpuinfo |grep "physical id" | sort | uniq | wc -l
1
?
#查看每個物理CPU中的core的個數
[root@localhost ~]# cat /proc/cpuinfo |grep "cpu cores" | uniq
cpu cores :2
?
查看邏輯物理CPU的個數
[root@localhost ~]# cat /proc/cpuinfo| grep"processor"| wc -l
2
?
查看CPU信息(型號)
[root@localhost ~]# cat /proc/cpuinfo |grep name | cut -f2 -d: | uniq -c
?????2? Intel(R) Core(TM) i5-3230M CPU@ 2.60GHz
?
查看內存信息
[root@localhost ~]# cat /proc/meminfo
?
?總結
以上是生活随笔為你收集整理的02_Nginx基本配置与参数说明 + 辅助命令的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 征信白户可以网贷吗
- 下一篇: 04_Nginx命令行参数,控制信号,N