生活随笔
收集整理的這篇文章主要介紹了
nginx使用整理
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
我的主頁(yè): isfantasy.com
前言
Nginx (engine x) 是一款輕量級(jí)的 Web 服務(wù)器 、反向代理服務(wù)器及電子郵件(IMAP/POP3)代理服務(wù)器。反向代理(Reverse Proxy)方式是指以代理服務(wù)器來接受 internet 上的連接請(qǐng)求,然后將請(qǐng)求轉(zhuǎn)發(fā)給內(nèi)部網(wǎng)絡(luò)上的服務(wù)器,并將從服務(wù)器上得到的結(jié)果返回給 internet 上請(qǐng)求連接的客戶端,此時(shí)代理服務(wù)器對(duì)外就表現(xiàn)為一個(gè)反向代理服務(wù)器。
常用命令
nginx -s stop 快速關(guān)閉Nginx,可能不保存相關(guān)信息,并迅速終止web服務(wù)。
nginx -s quit 平穩(wěn)關(guān)閉Nginx,保存相關(guān)信息,有安排的結(jié)束web服務(wù)。
nginx -s reload 因改變了Nginx相關(guān)配置,需要重新加載配置而重載。
nginx -s reopen 重新打開日志文件。
nginx -c filename 為 Nginx 指定一個(gè)配置文件,來代替缺省的。
nginx -t 不運(yùn)行,而僅僅測(cè)試配置文件。nginx 將檢查配置文件的語法的正確性,并嘗試打開配置文件中所引用到的文件。
nginx -v 顯示 nginx 的版本。
nginx -V 顯示 nginx 的版本,編譯器版本和配置參數(shù)。
實(shí)例
假如我現(xiàn)在擁有域名:www.fantasy.com,并且將其指向我的公網(wǎng)服務(wù)器A:210.42.42.42,nginx目錄結(jié)構(gòu)如下
nginx/
├── conf.d
│ ├── fantasy_http.conf
│ ├── ......
│ └── fantasy_static.conf
├── default.d
├── fastcgi.conf
├── fastcgi.conf.default
├── fastcgi_params
├── fastcgi_params.default
├── koi-utf
├── koi-win
├── mime.types
├── mime.types.default
├── nginx.conf
├── nginx.conf.default
├── nginx.conf.https
├── scgi_params
├── scgi_params.default
├── uwsgi_params
├── uwsgi_params.default
└── win-utf
http 反向代理配置
需求: 訪問www.fantasy.com(默認(rèn)訪問A的80端口)時(shí)會(huì)訪問到tomcat服務(wù)配置文件: nginx.conf
#運(yùn)行用戶
#user somebody;#啟動(dòng)進(jìn)程,通常設(shè)置成和cpu的數(shù)量相等
worker_processes 1;#全局錯(cuò)誤日志
error_log /var/log/nginx/error.log;
error_log /var/log/nginx/notice.log notice;
error_log /var/log/nginx/info.log info;#PID文件,記錄當(dāng)前啟動(dòng)的nginx的進(jìn)程ID
pid /etc/nginx/nginx.pid;#工作模式及連接數(shù)上限
events {worker_connections 1024; #單個(gè)后臺(tái)worker process進(jìn)程的最大并發(fā)鏈接數(shù)
}#設(shè)定http服務(wù)器,利用它的反向代理功能提供負(fù)載均衡支持
http {#設(shè)定mime類型(郵件支持類型),類型由mime.types文件定義include /etc/nginx/mime.types;default_type application/octet-stream;#設(shè)定日志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 /var/log/nginx/access.log main;rewrite_log on;#sendfile 指令指定 nginx 是否調(diào)用 sendfile 函數(shù)(zero copy 方式)來輸出文件,對(duì)于普通應(yīng)用,#必須設(shè)為 on,如果用來進(jìn)行下載等應(yīng)用磁盤IO重負(fù)載應(yīng)用,可設(shè)置為 off,以平衡磁盤與網(wǎng)絡(luò)I/O處理速度,降低系統(tǒng)的uptime.sendfile on;#tcp_nopush on;#連接超時(shí)時(shí)間keepalive_timeout 120;tcp_nodelay on;#gzip壓縮開關(guān)#gzip on;#設(shè)定實(shí)際的服務(wù)器列表upstream fantasy_tomcat{server 127.0.0.1:8080;}#HTTP服務(wù)器server {#監(jiān)聽80端口,80端口是知名端口號(hào),用于HTTP協(xié)議listen 80;#定義使用www.xx.com訪問server_name www.fantasy.com;#首頁(yè)#index index.html#指向webapp的目錄#root /home/fantasy/webapp;#編碼格式charset utf-8;#代理配置參數(shù)proxy_connect_timeout 180;proxy_send_timeout 180;proxy_read_timeout 180;proxy_set_header Host $host;proxy_set_header X-Forwarder-For $remote_addr;#反向代理的路徑(和upstream綁定),location 后面設(shè)置映射的路徑location / {proxy_pass http://fantasy_tomcat;}#靜態(tài)文件,nginx自己處理location ~ ^/(images|javascript|js|css|flash|media|static)/ {root /home/fantasy/webapp/views;#過期30天,靜態(tài)文件不怎么更新,過期可以設(shè)大一點(diǎn),如果頻繁更新,則可以設(shè)置得小一點(diǎn)。expires 30d;}#設(shè)定查看Nginx狀態(tài)的地址location /NginxStatus {stub_status on;access_log on;auth_basic "NginxStatus";auth_basic_user_file conf/htpasswd;}#禁止訪問 .htxxx 文件location ~ /\.ht {deny all;}#錯(cuò)誤處理頁(yè)面(可選擇性配置)#error_page 404 /404.html;#error_page 500 502 503 504 /50x.html;#location = /50x.html {# root html;#}}
}
效果 訪問www.fantasy.com便可訪問到你的tomcat服務(wù)
優(yōu)化配置文件
需求: 如果所有配置都寫在nginx.conf內(nèi)會(huì)很臃腫,現(xiàn)在在nginx目錄下創(chuàng)建conf.d目錄,用來存放額外配置,該目錄下所有conf后綴都會(huì)直接生效,實(shí)現(xiàn)上述需求配置: 創(chuàng)建配置文件fantasy_tomcat.conf:
## Basic reverse proxy server ##
upstream fantasy_tomcat{server 127.0.0.1:8080;
}
server {#監(jiān)聽80端口,80端口是知名端口號(hào),用于HTTP協(xié)議listen 80;#定義使用www.xx.com訪問server_name www.fantasy.com;#首頁(yè)#index index.html#指向webapp的目錄#root /home/fantasy/webapp;#編碼格式charset utf-8;#代理配置參數(shù)proxy_connect_timeout 180;proxy_send_timeout 180;proxy_read_timeout 180;proxy_set_header Host $host;proxy_set_header X-Forwarder-For $remote_addr;#反向代理的路徑(和upstream綁定),location 后面設(shè)置映射的路徑location / {proxy_pass http://fantasy_tomcat;}#靜態(tài)文件,nginx自己處理location ~ ^/(images|javascript|js|css|flash|media|static)/ {root /home/fantasy/webapp/views;#過期30天,靜態(tài)文件不怎么更新,過期可以設(shè)大一點(diǎn),如果頻繁更新,則可以設(shè)置得小一點(diǎn)。expires 30d;}#設(shè)定查看Nginx狀態(tài)的地址location /NginxStatus {stub_status on;access_log on;auth_basic "NginxStatus";auth_basic_user_file conf/htpasswd;}#禁止訪問 .htxxx 文件location ~ /\.ht {deny all;}#錯(cuò)誤處理頁(yè)面(可選擇性配置)#error_page 404 /404.html;#error_page 500 502 503 504 /50x.html;#location = /50x.html {# root html;#}}
修改 nginx.conf:
#運(yùn)行用戶
#user somebody;#啟動(dòng)進(jìn)程,通常設(shè)置成和cpu的數(shù)量相等
worker_processes 1;#全局錯(cuò)誤日志
error_log /var/log/nginx/error.log;
error_log /var/log/nginx/notice.log notice;
error_log /var/log/nginx/info.log info;#PID文件,記錄當(dāng)前啟動(dòng)的nginx的進(jìn)程ID
pid /etc/nginx/nginx.pid;#工作模式及連接數(shù)上限
events {worker_connections 1024; #單個(gè)后臺(tái)worker process進(jìn)程的最大并發(fā)鏈接數(shù)
}#設(shè)定http服務(wù)器,利用它的反向代理功能提供負(fù)載均衡支持
http {#設(shè)定mime類型(郵件支持類型),類型由mime.types文件定義include /etc/nginx//mime.types;default_type application/octet-stream;#設(shè)定日志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 /var/log/nginx/access.log main;rewrite_log on;#sendfile 指令指定 nginx 是否調(diào)用 sendfile 函數(shù)(zero copy 方式)來輸出文件,對(duì)于普通應(yīng)用,#必須設(shè)為 on,如果用來進(jìn)行下載等應(yīng)用磁盤IO重負(fù)載應(yīng)用,可設(shè)置為 off,以平衡磁盤與網(wǎng)絡(luò)I/O處理速度,降低系統(tǒng)的uptime.sendfile on;#tcp_nopush on;#連接超時(shí)時(shí)間keepalive_timeout 120;tcp_nodelay on;include /etc/nginx/conf.d/*.conf; #在這里引入conf.d文件夾下所有conf后綴的配置文件#gzip壓縮開關(guān)#gzip on;
}
https 反向代理配置
需求: 使網(wǎng)站可以通過https(443端口)協(xié)議訪問,需要你有安全證書,在 nginx.conf 中你需要指定證書和它對(duì)應(yīng)的 key配置: conf.d/fantasy_https.conf
#HTTP服務(wù)器server {#監(jiān)聽443端口。443為知名端口號(hào),主要用于HTTPS協(xié)議listen 443 ssl;#定義使用www.xx.com訪問server_name www.fantasy.com;#ssl證書文件位置(常見證書文件格式為:crt/pem)ssl_certificate cert.pem;#ssl證書key位置ssl_certificate_key cert.key;#ssl配置參數(shù)(選擇性配置)ssl_session_cache shared:SSL:1m;ssl_session_timeout 5m;#數(shù)字簽名,此處使用MD5ssl_ciphers HIGH:!aNULL:!MD5;ssl_prefer_server_ciphers on;location / {root /root;index index.html index.htm;}}
負(fù)載均衡配置
需求: 在210.42.42.42同局域網(wǎng)下有三臺(tái)應(yīng)用服務(wù)器服務(wù)器192.168.1.1、192.168.1.2、192.168.1.3,三臺(tái)服務(wù)器的80端口均運(yùn)行一個(gè)商城應(yīng)用.為了緩解服務(wù)器壓力,當(dāng)用戶訪問buy.fantasy.com時(shí),需要將用戶請(qǐng)求分發(fā)到內(nèi)網(wǎng)的三個(gè)服務(wù)器上,服務(wù)器A只做請(qǐng)求分發(fā).配置文件: conf.d/fantasy_balance_buy.conf
http {#設(shè)定mime類型,類型由mime.type文件定義include /etc/nginx/mime.types;default_type application/octet-stream;#設(shè)定日志格式access_log /var/log/nginx/access.log;#設(shè)定負(fù)載均衡的服務(wù)器列表upstream fantasy_balance_buy {#weigth參數(shù)表示權(quán)值,權(quán)值越高被分配到的幾率越大server 192.168.1.1:80 weight=1;server 192.168.1.2:80 weight=2;server 192.168.1.3:80 weight=3;}#HTTP服務(wù)器server {#偵聽80端口listen 80;#定義使用buy.fantasy.com訪問server_name buy.fantasy.com;#對(duì)所有請(qǐng)求進(jìn)行負(fù)載均衡請(qǐng)求location / {#root /root; #定義服務(wù)器的默認(rèn)網(wǎng)站根目錄位置#index index.html index.htm; #定義首頁(yè)索引文件的名稱proxy_pass http://fantasy_balance_buy ;#請(qǐng)求轉(zhuǎn)向load_balance_server 定義的服務(wù)器列表#以下是一些反向代理的配置(可選擇性配置)#proxy_redirect off;proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;#后端的Web服務(wù)器可以通過X-Forwarded-For獲取用戶真實(shí)IPproxy_set_header X-Forwarded-For $remote_addr;proxy_connect_timeout 90; #nginx跟后端服務(wù)器連接超時(shí)時(shí)間(代理連接超時(shí))proxy_send_timeout 90; #后端服務(wù)器數(shù)據(jù)回傳時(shí)間(代理發(fā)送超時(shí))proxy_read_timeout 90; #連接成功后,后端服務(wù)器響應(yīng)時(shí)間(代理接收超時(shí))proxy_buffer_size 4k; #設(shè)置代理服務(wù)器(nginx)保存用戶頭信息的緩沖區(qū)大小proxy_buffers 4 32k; #proxy_buffers緩沖區(qū),網(wǎng)頁(yè)平均在32k以下的話,這樣設(shè)置proxy_busy_buffers_size 64k; #高負(fù)荷下緩沖大小(proxy_buffers*2)proxy_temp_file_write_size 64k; #設(shè)定緩存文件夾大小,大于這個(gè)值,將從upstream服務(wù)器傳client_max_body_size 10m; #允許客戶端請(qǐng)求的最大單文件字節(jié)數(shù)client_body_buffer_size 128k; #緩沖區(qū)代理緩沖用戶端請(qǐng)求的最大字節(jié)數(shù)}}
}
效果: 當(dāng)用戶訪問buy.fantasy.com時(shí),其請(qǐng)求會(huì)按照幾率分發(fā)到子網(wǎng)下的三個(gè)服務(wù)器.
根據(jù)請(qǐng)求路徑映射服務(wù)
**需求:**服務(wù)器A上分別運(yùn)行三個(gè)服務(wù):10001端口的服務(wù)service1、10002端口的服務(wù)service2、10003端口的服務(wù)service3,具體需求如下:
請(qǐng)求結(jié)果 fantasy.com/service1 10001端口的服務(wù)service1 fantasy.com/service2 10002端口的服務(wù)service2 fantasy.com/service3 10003端口的服務(wù)service3
配置: conf.d/fantasy_service.conf
http {#此處省略一些基本配置upstream fantasy_service1{server fantasy.com:10001;}upstream fantasy_service2{server fantasy.com:10002;}upstream fantasy_service2{server fantasy.com:10002;}server {#此處省略一些基本配置#默認(rèn)指向product的serverlocation / {proxy_pass http://fantasy_service1;}location /service1/{proxy_pass http://fantasy_service1;}location /service2/ {proxy_pass http://fantasy_service2;}location /service3/ {proxy_pass http://fantasy_service3;}}
}
靜態(tài)站點(diǎn)配置
需求: 服務(wù)器A上/data/webapp文件夾下有靜態(tài)網(wǎng)頁(yè)html/css/js/img,需要通過static.fantasy.com的方式訪問到這些資源配置: conf.d/fantasy_static.conf
worker_processes 1;events {worker_connections 1024;
}http {include mime.types;default_type application/octet-stream;sendfile on;keepalive_timeout 65;gzip on;gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/javascript image/jpeg image/gif image/png;gzip_vary on;server {listen 80;server_name static.fantasy.com;location / {root /data/img;index index.html;#轉(zhuǎn)發(fā)任何請(qǐng)求到 index.html}}
}
效果: /data/img文件下有圖片fantasy.png,可通過img.fantasy.com/fantasy.png訪問
搭建文件服務(wù)器
autoindex 開啟可以顯示目錄,默認(rèn)不開啟 autoindex_exact_size 開啟可以顯示文件的大小 autoindex_localtime 開啟可以顯示文件的修改時(shí)間 root 用來設(shè)置開放為文件服務(wù)的根路徑 charset 設(shè)置為 charset utf-8,gbk;,可以避免中文亂碼問題(windows 服務(wù)器下設(shè)置后,依然亂碼?)
autoindex on;# 顯示目錄
autoindex_exact_size on;# 顯示文件大小
autoindex_localtime on;# 顯示文件時(shí)間server {charset utf-8,gbk; # windows 服務(wù)器下設(shè)置后,依然亂碼,暫時(shí)無解listen 9050 default_server;listen [::]:9050 default_server;server_name _;root /share/fs;
}
跨域解決方案
解決跨域問題一般有兩種思路:
CORS:在后端服務(wù)器設(shè)置 HTTP 響應(yīng)頭,把你需要運(yùn)行訪問的域名加入加入 Access-Control-Allow-Origin 中。 jsonp:把后端根據(jù)請(qǐng)求,構(gòu)造 json 數(shù)據(jù),并返回,前端用 jsonp 跨域。 需求: www.fantasy.com 網(wǎng)站是由一個(gè)前端 app ,一個(gè)后端 app 組成的。前端端口號(hào)為 10000, 后端端口號(hào)為 10001。 配置: conf.d/enable-cors.conf
# allow origin list
set $ACAO '*';# set single origin
if ($http_origin ~* (www.fantasy.com)$) {set $ACAO $http_origin;
}if ($cors = "trueget") {add_header 'Access-Control-Allow-Origin' "$http_origin";add_header 'Access-Control-Allow-Credentials' 'true';add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';add_header 'Access-Control-Allow-Headers' 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
}if ($request_method = 'OPTIONS') {set $cors "${cors}options";
}if ($request_method = 'GET') {set $cors "${cors}get";
}if ($request_method = 'POST') {set $cors "${cors}post";
}
總結(jié)
以上是生活随笔 為你收集整理的nginx使用整理 的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔 推薦給好友。