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