Nginx 快速入門
CenteOS 安裝
下載地址
相關軟件
已經安裝了的就可以跳過
1. 安裝pcre
# yum install -y pcre*
2. 安裝openssl # yum install -y openssl*
3. 安裝make # yum install -y make
安裝Nginx
# tar -zxvf nginx-1.10.3.tar.gz # ./configure --with-http_ssl_module --with-http_stub_status_module --with-pcre --prefix=/usr/local/nginx-1.10.3 # make && make install # ln -s /usr/local/nginx-1.10.3 nginx # ln -s /usr/local/mysql/bin/mysql /usr/bin
參數說明:
–with-http_stub_status_module:支持 nginx 狀態查詢
–with-http_ssl_module:支持 https
–with-pcre:爲了支持 rewrite 重寫功能,必須制定 pcre
Mac安裝
已安裝過homebrew的直接跳過homebrew的安裝
$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" \\ $ brew search nginx $ brew install nginx
安裝完以後,可以在終端輸出的信息裏看到一些配置路徑:
/usr/local/etc/nginx/nginx.conf (配置文件路徑)
/usr/local/var/www (服務器默認路徑)
/usr/local/Cellar/nginx/1.10.3 (安裝路徑)
訪問localhost:8080,成功說明安裝好了
運行、關閉
運行:
# nginx
測試
# curl -s http://localhost
關閉
# nginx -s stop
修改配置後重置
# nginx -s reload
測試配置
# nginx -t
Nginx配置
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; 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; # } #} include servers/*; }
server{} 代碼相當於一個代理服務器,可以配置多個
listen:表示當前的代理服務器監聽的端口,默認的是監聽8080端口
注意,如果我們配置了多個server,這個listen要配置不一樣,不然就不能確定轉到哪裏去了
server_name:表示監聽到之後需要轉到哪裏去,這裏我們直接轉到本地localhost,這時是直接到nginx文件夾內
location:表示匹配的路徑,這裏配置了/表示所有請求都被匹配到這裏,支持正則表達式通配,如果多個location配置地址重合,只會匹配第一個
root:裏面配置了root這時表示當匹配這個請求的路徑時,將會在這個文件夾內尋找相應的文件,這對靜態文件伺服很有用
index:當沒有指定主頁時,默認會選擇這個指定的文件,它可以有多個,並按順序來加載,如果第一個不存在,則找第二個,依此類推
error_page是代表錯誤的頁面
配置請求到tomcat
修改配置
proxy_pass,表示代理路徑,相當於轉發
server_name localhost:8080; location / { proxy_pass http://localhost:8080; }
$ nginx -t
$ nginx -s reload
配置動靜分離
#動態頁面交給http://tdt_wugk,也即我們之前在nginx.conf定義的upstream tdt_wugk 均衡 location ~ .*\.(php|jsp|do)?$ { #設置主機頭和客戶端真實地址,以便服務器獲取客戶端真實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; #禁用緩存 proxy_buffering off; proxy_pass http://localhost:8080; } #配置Nginx動靜分離,定義的靜態頁面直接從Nginx發佈目錄讀取。 location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$ { root /usr/local/var/www/; #expires定義用戶瀏覽器緩存的時間爲3天,如果靜態頁面不常更新,可以設置更長,這樣可以節省帶寬和緩解服務器的壓力 expires 3d; }
配置負載均衡
upstream local_tomcat { #ip_hash; #是否使用ip_hash server 192.168.1.101:8080; server 192.168.1.102:8080; server 192.168.1.103:8080; } server{ location / { #設置主機頭和客戶端真實地址,以便服務器獲取客戶端真實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; #禁用緩存 proxy_buffering off; proxy_pass http://local_tomcat; } ..... }
添加upstream,而直接在proxy_pass裏面直接用http://+upstream的名稱來使用。
upstream默認是以輪詢的方式實現負載均衡,這種方式中,每個請求按時間順序逐一分配到不同的後端服務器,如果後端服務器down掉,能自動剔除。
ip_hash方式:每個請求按訪問ip的hash結果分配,這樣每個訪客固定訪問一個後端服務器,可以解決session的問題。另一種方式可以使用nginx sticky實現基於cookie的負載均衡。
upstream中的server元素必須要注意,不能加http:,但proxy_pass中必須加。