1、nginx的介紹php
nginx是由俄羅斯人開發的一款高性能的http和反向代理服務器,也能夠用來做爲郵件代理。相比較於其餘的服務器,具備佔用內存少,穩定性高等優點css
Nginx相關地址html
源碼:https://trac.nginx.org/nginx/browsernginx
官網:http://www.nginx.org/web
2、nginx的配置瀏覽器
nginx經常使用命令:緩存
nginx -s stop 快速關閉Nginx,可能不保存相關信息,並迅速終止web服務。服務器
nginx -s quit 平穩關閉Nginx,保存相關信息,有安排的結束web服務。 session
nginx -s reload 因改變了Nginx相關配置,須要從新加載配置而重載。 app
nginx -s reopen 從新打開日誌文件。
nginx -c filename 爲 Nginx 指定一個配置文件,來代替缺省的。
nginx -t 不運行,而僅僅測試配置文件。nginx 將檢查配置文件的語法的正確性,並嘗試打開配置文件中所引用到的文件。
nginx -v 顯示 nginx 的版本。 nginx -V 顯示 nginx 的版本,編譯器版本和配置參數。
啓動服務:
或者直接運行 nginx.bat 如圖:
若是未啓動服務成功能夠查看logs文件下error文件日誌
常見錯誤信息
No mapping for the Unicode character exists in the target multi-byte code page:表示文件目錄中含有中文字符,建議直接放在C盤根目錄下
An attempt was made to access a socket in a way forbidden by its access permissions:表示80端口,能夠去配置將80端口更改成別的端口或者去註冊表裏面禁用80端口,這裏不建議去註冊表禁用80端口,由於禁用後iis全部的站點將會沒法啓動
這裏是在本機測試,先去iis新建兩個站點,端口分別爲8081和8082
3、nginx的配置介紹
nginx主要配置文件是在conf/nginx.conf裏面
主要配置信息以下
#user nobody; worker_processes 4; #nginx進程數,建議設置爲等於CPU總核心數 #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; #服務器集羣名稱爲Jq_one upstream ngintest.com{ server 127.0.0.1:8081 weight=4; server 127.0.0.1:8082 weight=4; } server { listen 8088; server_name ngintest.com; #charset koi8-r; #access_log logs/host.access.log main; location / { root html; index index.aspx index.html index.htm; #指向集羣名稱爲Jq_one proxy_pass http://ngintest.com; #設置主機頭和客戶端真實地址,以便服務器獲取客戶端真實IP proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } #靜態資源緩存設置 location ~ \.(jpg|png|jpeg|bmp|gif|swf|css)$ { expires 30d; root /nginx-1.9.3/html;#root: break; } #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; # } #} }
upstream ngintest.com{
server 127.0.0.1:8081 weight=4; server 127.0.0.1:8082 weight=4; }
表示要負載均衡的站點,多個站點可在裏面增減,
weight表示訪問權重值,值越大表示訪問到的概率越大
listen 8088;
表示nginx要監聽的端口,這裏因爲iis默認會佔用80端口,因此這裏要改爲除80之外端口
server_name ngintest.com;
服務器名稱,這裏服務器名稱要和upstream後面的名稱一致
所有配置好後重啓nginx服務
瀏覽器訪問:127.0.0.1:8088,屢次刷新會在兩個站點來回切換