一:簡介php
Nginx是一款輕量級的Web 服務器/反向代理服務器及電子郵件(IMAP/POP3)代理服務器,並在一個BSD-like 協議下發行。由俄羅斯的程序設計師Igor Sysoev所開發,供俄國大型的入口網站及搜索引擎Rambler(俄文:Рамблер)使用。其特色是佔有內存少,併發能力強,事實上nginx的併發能力確實在同類型的網頁服務器中表現較好。css
二:這次安裝的是一個運行在windows上的反向代理服務器,主要和iis配合使用html
直接啓動exe文件便可nginx
注意: 文件夾不能含有中文,不然會有錯誤
數據庫
三:建立2個測試的文件,發佈在iis上windows
四:修改nginx.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; upstream www.aaa.com { server 127.0.0.1:8081 weight=1; #第一個測試網站 server 127.0.0.1:8082 weight=1; #第二個測試網站 } server { listen 8080;#這個原來是80端口,若是80已經被佔用須要進行修改 server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { root html; index index.html index.htm; proxy_pass http://www.aaa.com;#反向代理指向地址 } #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; # } #} }
而後直接啓動便可,可是須要注意以上內容中的空格,不然啓動失敗服務器
1.若是站點使用了session,請求平均分配到兩個站點,那麼必然存在session共享問題,該如何解決?session
upstream Jq_one{
server 127.0.0.1:8082 ;
server 127.0.0.1:9000 ;
ip_hash;
}併發
2.因爲請求是通過nginx轉發過來的,能夠在代碼裏面獲取到用戶請求的實際ip地址嗎?
#設置主機頭和客戶端真實地址,以便服務器獲取客戶端真實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;
代碼裏面經過Request.Headers["X-Real-IP"],就能獲取到真實ip
3 nginx實現靜態文件(image,js,css)緩存
這是index頁面的代碼 <li><img src="/images/1.jpg"/></li>
主要參考文章:http://www.cnblogs.com/yanweidie/archive/2015/07/19/4658136.html