轉自:https://www.jianshu.com/p/717f2b88d057php
Nginx是一個高性能的HTTP和反向代理服務器(反向代理就是一般所說的web服務器加速,它是一種經過在繁忙的web服務器和internet之間增長一個高速的web緩衝服務器來下降實際的web服務器的負載),Nginx由俄羅斯程序員利用C語言開發,以穩定、低系統資源消耗聞名,騰訊、百度、阿里、京東、網易等均有部署使用。此外,在高鏈接併發的狀況下,Nginx是Apache的不錯替代品,其可以支持高達50000個併發鏈接數的響應。css
一、編譯工具和庫文件的安裝html
yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel
二、prce的安裝
如下假設咱們安裝在src文件夾中
下載:node
[root@bogon src]# wget http://downloads.sourceforge.net/project/pcre/pcre/8.35/pcre-8.35.tar.gz
解壓:nginx
[root@bogon src]# tar zxvf pcre-8.35.tar.gz
安裝:c++
cd pcre-8.35 ./configure make && make install
三、Nginx的安裝
下載:程序員
[root@bogon src]# wget http://nginx.org/download/nginx-1.6.2.tar.gz
安裝:web
[root@bogon nginx-1.6.2]# ./configure --prefix=/usr/local/webserver/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre=/usr/local/src/pcre-8.35 [root@bogon nginx-1.6.2]# make [root@bogon nginx-1.6.2]# make install
最後進入響應的目錄執行nginx可執行文件便可。瀏覽器
Windows下只須要下載解壓便可使用,下載地址http://nginx.org/en/download.html
運行nginx.exe,便可啓動服務,在瀏覽器中打開可看到如下畫面,這說明Nginx已經運行起來了。ruby
要運行起本身的網站咱們還須要對Nginx作一些配置,在nginx文件夾的子文件夾conf下的nginx.config文件就是Nginx的配置文件,其文件內容以下:
#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的用戶名 #user nobody; #cpu數,通常設置成和服務器的cpu數一致 worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #進程id #pid logs/nginx.pid; events { worker_connections 1024; } 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壓縮(網頁速度優化很是有用,開啓後一般能夠達到70%的壓縮率) #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; } #定義錯誤提示頁面,你還能夠在這裏添加500,403等,以空格分開 #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中添加一個server類,以下:
server {
listen 80; #域名 server_name huruji3.com www.huruji3.com; location / { #node.js應用的端口 proxy_pass http://127.0.0.1:3000; root blog; } #靜態文件交給Nginx直接處理 location ~ *^.+\.(css | js | txt | swf | mp4)$ { root E:\huruji\blog\wechat_v1.1\public; access_log off; expires 24h; } }
固然,咱們爲了最大化的利用域名,咱們有時須要更多的使用二級域名,以運行更多的應用,一樣咱們只要再添加一個類:
server {
listen 80; #域名 server_name blog.huruji3.com; location / { #node.js應用的端口 proxy_pass http://127.0.0.1:3001; root blog; } #靜態文件交給Nginx直接處理 location ~ *^.+\.(css | js | txt | swf | mp4)$ { root E:\huruji\blog\wechat_v1.1\public; access_log off; expires 24h; } }
這裏說明一下,咱們利用二級域名是一種充分利用的域名資源的方法,一樣利用路徑也能夠,這和使用的服務器內部採用的映射方式有關,好比院網和工做室網站對外表現就是不一樣的網站,可是工做室網站的/hope只是一個路徑而已,Nginx不能根據路徑,可使用二級域名使得不一樣應用運行在同一個一級域名下。
如下的Nginx配置,打開不一樣域名也就訪問了不一樣網站:
#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 127.0.0.1; #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 80; server_name huruji3.com www.huruji3.com; location / { proxy_pass http://127.0.0.1:3000; root blog; } #location ~ *^.+\.*$ { # root E:\huruji\blog\wechat_v1.1\public; # access_log off; # expires 24h; #} } server { listen 80; server_name blog.huruji3.com; location / { proxy_pass http://127.0.0.1:3001; root blog; } #location ~ *^.+\.*$ { # root E:\huruji\blog\wechat_v1.1\public; # access_log off; # expires 24h; #} } # 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; # } #} }
瀏覽器輸入不一樣地址,也就訪問了不一樣網站應用: