版權聲明:本文爲博主原創文章,未經博主容許不得轉載。php
一. 下載並安裝Nginxhtml
去Nginx官網下載java
我這裏選取nginx/Windows-1.10.3版本,下載後解壓出來便可,解壓出來的路徑不能含有中文node
我解壓後將其放置的路徑以下python
2、開始運行linux
在當前目錄下按住shift+鼠標右鍵,選擇「在此處打開命令窗口」,而後輸入start nginxnginx
此時,就能夠進入瀏覽器輸入訪問地址,http://127.0.0.1/或者http://localhost/便可訪問web
3、配置文件講解正則表達式
核心配置文件就是nginx.conf,該文件位於conf目錄下,大部分狀況下咱們就是修改該文件的配置瀏覽器
該文件的原始配置以下:
#user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events { worker_connections 1024; } http { 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 on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { root html; index index.html index.htm; } #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; } # 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 # #location ~ /\.ht { # deny all; #} } # 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; # } #} }
其中#表明註釋
nginx咱們最主要的做用是拿來作反向代理和負載均衡,這個我後面會着重講解。同時它仍是一個web服務器,與咱們經常使用的Apache、tomcat、IIS同樣,也能夠用來託管web服務。
本章先暫時介紹下該配置文件中的幾個重要參數,後面會對nginx部署php和Python項目再進行着重講解,至於java的項目一般是tomcat+nginx同時進行配置,nginx用來作負載均衡和處理靜態頁。
一、定義Nginx運行的用戶和用戶組
#user nobody;
二、nginx進程數,建議設置爲等於CPU總核心數
worker_processes 1;
三、全局錯誤日誌定義類型,[ debug | info | notice | warn | error | crit ]
#error_log logs/error.log notice; #error_log logs/error.log info;
四、進程文件
#pid logs/nginx.pid;
五、工做模式與鏈接數上限:worker_connections是單個後臺worker process進程的最大併發連接數,併發總數是 worker_processes 和 worker_connections 的乘積, 即 max_clients = worker_processes * worker_connections
events { worker_connections 1024; }
六、http下的一些配置及其意義
include mime.types; #文件擴展名與文件類型映射表 default_type application/octet-stream; #默認文件類型 sendfile on; #開啓高效文件傳輸模式,sendfile指令指定nginx是否調用sendfile函數來 輸出文件,對於普通應用設爲 on,若是用來進行下載等應用磁盤IO重負載應用,可設置 爲off,以平衡磁盤與網絡I/O處理速度,下降系統的負載。注意:若是圖片顯示不正常 把這個改爲off。 autoindex on; #開啓目錄列表訪問,合適下載服務器,默認關閉。 tcp_nopush on; #防止網絡阻塞 tcp_nodelay on; #防止網絡阻塞 keepalive_timeout 120; #長鏈接超時時間,單位是秒 gzip on; #開啓gzip壓縮輸出
七、server虛擬主機的相關配置
咱們平時配置各種服務器,配置最多的就是這些地方了
好比:
http{ #虛擬主機1 server{ listen 80; #監聽端口,基於IP配置的時候變動此處,好比192.168.1.100:8080; server_name www.xdw.com; #主機域名,實際項目發佈的話,填公網上的域名,本地部署的話,能夠在C:\Windows\System32\drivers\etc\hosts文件中添加IP和域名的映射 location / { #映射解析,/表明根路徑,此處解析還有正則表達式的解析方式,具體請參考http://tengine.taobao.org/nginx_docs/cn/docs/http/ngx_http_core_module.html#location root E:/xdw/0221; #工程所在路徑 index index.html index.htm; #首頁(默認頁) } } #虛擬主機2,能夠同時配置多個虛擬主機 server{ listen 8080; server_name localhost; location / { root D:/xiangmu/txym_web; index index.html index.htm; } } }
看到這個虛擬主機的配置,相信配置過tomcat或者Apache的人都很熟悉的感受。此篇就到此結束,下面還會更新linux下的配置,php和python項目的部署,反向代理和負載均衡,配合tomcat部署java項目。