Nginx概念:反向代理(不關(guān)心服務(wù)器是誰)服務(wù)器
一、Nginx常用命令:
關(guān)閉nginx:nginx -s stop/quit? ?
stop是快速關(guān)閉员魏,quit正常關(guān)閉(等請求走完才關(guān))
重新加載:nginx -s reload
二丑蛤、nginx配置文件組成:
? ? ?1、全局塊:影響nginx服務(wù)器整體運(yùn)行的配置指令
? ? ?2撕阎、events塊:影響nginx服務(wù)器與用戶的網(wǎng)絡(luò)連接
? ? ?3受裹、http塊:
? ? ? ? ? a、http全局塊:文件引入(include)虏束,MIME-TYPE定義棉饶,日志自定義,連接超時(shí)時(shí)間镇匀,負(fù)載均衡邏輯(upstream)等:
????????負(fù)載均衡常見策略:
????????????1)輪詢(默認(rèn))
????????????2)權(quán)重(weight=n)
????????????3)ip_hash匹配照藻,訪客的ip和服務(wù)器ip匹配,可以解決session不一致的問題
????????????4)fair(第三方)汗侵,響應(yīng)時(shí)間越短的分配訪問次數(shù)越多
? ? ? ? ? b幸缕、server塊:
????????????location方式:
????????????????1)=? 精確匹配
????????????????2)~? 正則匹配,區(qū)分大小寫
????????????????3)~* 正則匹配晃择,不區(qū)分大小寫
????????????????4)^~ 在前綴匹配中冀值,視作最長長度匹配
????????????匹配規(guī)則:
????????????????1)先查找 = 精確匹配,找到了就返回該條
????????????????2)查找最長前綴匹配^~宫屠,匹配到了就返回該條
????????????????3)沒有精確匹配列疗,和最長前綴匹配,“按順序”查找正則匹配浪蹂,~抵栈,~*兩種,找到任何一條匹配正則就返回
????????????????4)上面三者都沒坤次,等于用普通前綴長度匹配規(guī)則古劲,記錄下最長匹配的那條返回
????????????????5)/ 匹配全部,等于是兜底的最低級的前綴匹配
三缰猴、配置文件例子和注釋
include掃碼路徑下产艾,還有個(gè)依賴文件,可以參考
upstream zcy_bidding {
? ? server 127.0.0.1:8040;
}
upstream zcy_bidding-open {
? ? server 127.0.0.1:8049;
}
upstream zcy_project_manage {
? ? server 127.0.0.1:8022;
}
upstream zcy_bidding-entrust {
? ? #server 127.0.0.1:3000;? ?
? ? #server 172.16.101.50:80;
? ? #server 172.16.101.166:80;
? ? server 172.19.195.224:80;
}
upstream zcy_middle {
? server middle.test-shanghai.cai-inc.com;
}
upstream zcy_login {
? server login.test-shanghai.cai-inc.com;
}
upstream zcy_main {
? server test-shanghai.cai-inc.com;
}
server {
? ? listen 80;
? ? listen 443 ssl;
? ? server_name www.test.cai-inc.com, www.test-shanghai.cai-inc.com,bidding.dev-helion.cai-inc.com,bidding.dev-hecate.cai-inc.com,dev-hecate.cai-inc.com,test.cai-inc.com,bidding.test.cai-inc.com,test-shanghai.cai-inc.com,bidding.test-shanghai.cai-inc.com,www.dev-fulltime.cai-inc.com,project-manage.test-shanghai.cai-inc.com;
? ? error_page 404 403 /error;
? ? ssl_certificate? ? ? /usr/local/etc/nginx/conf.d/server.pem;
? ? ssl_certificate_key? /usr/local/etc/nginx/conf.d/privkey.pem;
? ? # access_log? /usr/local/etc/nginx/logs/bidding_access.log;
? ? # error_log? /usr/local/etc/nginx/logs/bidding_error.log;
? ? location = /favicon.ico {
? ? ? return? 404;
? ? }
? location ~ ^/assets/(.*)$ {
? ? ? #root /Volumes/zcy-bidding-front/public;
? ? ? #root /Users/yesensen/FrontProjects/zcy-project-manage-front/public;
? ? ? root /Users/luyun/Documents/code/zcy-bidding-front/public;
? ? ? expires 30d;
? ? ? access_log off;
? }
? location ~ ^/assets_bidding-open/(.*)$ {
? ? ? #root /Volumes/public;
? ? ? root /Users/luyun/Documents/code/web-bidding-open-front/public;
? ? ? expires 30d;
? ? ? access_log off;
? }
? ? ? ? ## 需要轉(zhuǎn)發(fā)給middle的
? ? location = /api/getDistrictByCodeWithoutDate {
? ? ? proxy_pass? ? ? ? ? http://zcy_middle;
? ? ? proxy_set_header? ? ? ? X-Real-IP $remote_addr;
? ? ? proxy_set_header? ? ? ? X-Forwarded-For $proxy_add_x_forwarded_for;
? ? ? proxy_set_header? ? ? ? Host $http_host;
? ? }
? ? location = /neoericBacklog/api/obtainBacklogHeadInfo {
? ? ? ? proxy_pass? ? ? ? ? http://zcy_middle;
? ? ? ? proxy_set_header? ? ? ? X-Real-IP $remote_addr;
? ? ? ? proxy_set_header? ? ? ? X-Forwarded-For $proxy_add_x_forwarded_for;
? ? ? ? proxy_set_header? ? Host $http_host;? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
? ? }
? ? location ~ ^/api/zoss/(.*)$ {
? ? ? proxy_pass http://zcy_middle;
? ? ? proxy_set_header? ? ? ? X-Real-IP $remote_addr;
? ? ? proxy_set_header? ? ? ? X-Forwarded-For $proxy_add_x_forwarded_for;
? ? ? proxy_set_header? ? ? ? Host $http_host;
? ? }
? ? location = /api/getGpcatalog/tree {
? ? ? proxy_pass? ? ? ? ? http://zcy_middle;
? ? ? proxy_set_header? ? ? ? X-Real-IP $remote_addr;
? ? ? proxy_set_header? ? ? ? X-Forwarded-For $proxy_add_x_forwarded_for;
? ? ? proxy_set_header? ? ? ? Host $http_host;
? ? }
? ? location = /api/zcy/common/getTreeNameByCode {
? ? ? proxy_pass? ? ? ? ? http://zcy_middle;
? ? ? proxy_set_header? ? ? ? X-Real-IP $remote_addr;
? ? ? proxy_set_header? ? ? ? X-Forwarded-For $proxy_add_x_forwarded_for;
? ? ? proxy_set_header? ? ? ? Host $http_host;
? ? }
? ? location = /api/district/getDistrictTreeWithPrivilegeInJson {
? ? ? proxy_pass? ? ? ? ? http://zcy_middle;
? ? ? proxy_set_header? ? ? ? X-Real-IP $remote_addr;
? ? ? proxy_set_header? ? ? ? X-Forwarded-For $proxy_add_x_forwarded_for;
? ? ? proxy_set_header? ? ? ? Host $http_host;
? ? }
? ? location = /middle/api/district/getDistrictTree {
? ? ? proxy_pass? ? ? ? ? http://zcy_middle;
? ? ? proxy_set_header? ? ? ? X-Real-IP $remote_addr;
? ? ? proxy_set_header? ? ? ? X-Forwarded-For $proxy_add_x_forwarded_for;
? ? ? proxy_set_header? ? ? ? Host $http_host;
? ? }
? ? location ~ ^/api/announcement/(.*)$ {
? ? ? proxy_pass? ? ? ? ? http://zcy_middle;
? ? ? proxy_set_header? ? ? ? X-Real-IP $remote_addr;
? ? ? proxy_set_header? ? ? ? X-Forwarded-For $proxy_add_x_forwarded_for;
? ? ? proxy_set_header? ? ? ? Host $http_host;
? ? }
? ? location ~ ^/announcement/api/(.*)$ {
? ? ? proxy_pass? ? ? ? ? http://zcy_middle;
? ? ? proxy_set_header? ? ? ? X-Real-IP $remote_addr;
? ? ? proxy_set_header? ? ? ? X-Forwarded-For $proxy_add_x_forwarded_for;
? ? ? proxy_set_header? ? ? ? Host $http_host;
? ? }
? ? location /api/user/checkSessionAliveForAllPlatformfromMember{
? ? ? proxy_pass http://zcy_login/api/user/checkSessionAliveForAllPlatformfromMember;
? ? ? proxy_set_header? ? ? ? X-Real-IP $remote_addr;
? ? ? proxy_set_header? ? ? ? X-Forwarded-For $proxy_add_x_forwarded_for;
? ? ? proxy_set_header? ? ? ? Host $http_host;
? ? }
? ? location /api/user/keepSessionAliveForAllPlatformfromMember{
? ? ? proxy_pass http://zcy_login/api/user/keepSessionAliveForAllPlatformfromMember;
? ? ? proxy_set_header? ? ? ? X-Real-IP $remote_addr;
? ? ? proxy_set_header? ? ? ? X-Forwarded-For $proxy_add_x_forwarded_for;
? ? ? proxy_set_header? ? ? ? Host $http_host;
? ? }
? ? location /sessonKickedOutByPlatForm{
? ? ? proxy_pass http://zcy_login/sessonKickedOutByPlatForm;
? ? ? proxy_set_header? ? ? ? X-Real-IP $remote_addr;
? ? ? proxy_set_header? ? ? ? X-Forwarded-For $proxy_add_x_forwarded_for;
? ? ? proxy_set_header? ? ? ? Host $http_host;
? ? }
? ? location ~ ^/api/common/(.*)$ {
? ? ? proxy_pass http://zcy_login;
? ? ? proxy_set_header? ? ? ? X-Real-IP $remote_addr;
? ? ? proxy_set_header? ? ? ? X-Forwarded-For $proxy_add_x_forwarded_for;
? ? ? proxy_set_header? ? ? ? Host $http_host;
? ? }
? ? location /middle/ {
? ? ? proxy_pass http://zcy_middle/;
? ? ? proxy_set_header? ? ? ? X-Real-IP $remote_addr;
? ? ? proxy_set_header? ? ? ? X-Forwarded-For $proxy_add_x_forwarded_for;
? ? ? proxy_set_header? ? ? ? Host $http_host;
? ? }
? ? location /biding/ {
? ? ? proxy_pass http://zcy_bidding;
? ? ? proxy_set_header? ? ? ? X-Real-IP $remote_addr;
? ? ? proxy_set_header? ? ? ? X-Forwarded-For $proxy_add_x_forwarded_for;
? ? ? proxy_set_header? ? ? ? Host $http_host;
? ? }
? ? location ~ ^/projectContract/(.*)$? {
? ? ? proxy_pass http://zcy_bidding;
? ? ? proxy_set_header? ? ? ? X-Real-IP $remote_addr;
? ? ? proxy_set_header? ? ? ? X-Forwarded-For $proxy_add_x_forwarded_for;
? ? ? proxy_set_header? ? ? ? Host $http_host;
? ? }
? ? location /api/bidding-open/ {
? ? ? proxy_pass http://zcy_bidding-open;
? ? ? proxy_set_header? ? ? ? X-Real-IP $remote_addr;
? ? ? proxy_set_header? ? ? ? X-Forwarded-For $proxy_add_x_forwarded_for;
? ? ? proxy_set_header? ? ? ? Host $http_host;
? ? ? add_header Cache-Control 'private, no-store, no-cache, must-revalidate';
? ? }
? ? location /bidding-open/ {
? ? ? proxy_pass http://zcy_bidding-open;
? ? ? proxy_set_header? ? ? ? X-Real-IP $remote_addr;
? ? ? proxy_set_header? ? ? ? X-Forwarded-For $proxy_add_x_forwarded_for;
? ? ? proxy_set_header? ? ? ? Host $http_host;
? ? ? add_header Cache-Control 'private, no-store, no-cache, must-revalidate';
? ? }
? ? location /procurement-open/ {
? ? ? proxy_pass http://zcy_bidding-open;
? ? ? proxy_set_header? ? ? ? X-Real-IP $remote_addr;
? ? ? proxy_set_header? ? ? ? X-Forwarded-For $proxy_add_x_forwarded_for;
? ? ? proxy_set_header? ? ? ? Host $http_host;
? ? ? add_header Cache-Control 'private, no-store, no-cache, must-revalidate';
? ? }
? ? location /api/projectmanage/ {
? ? ? proxy_pass http://zcy_project_manage;
? ? ? proxy_set_header? ? ? ? X-Real-IP $remote_addr;
? ? ? proxy_set_header? ? ? ? X-Forwarded-For $proxy_add_x_forwarded_for;
? ? ? proxy_set_header? ? ? ? Host $http_host;
? ? ? add_header Cache-Control 'private, no-store, no-cache, must-revalidate';? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
? ? }
? ? location ~ ^/projectmanage/(.*)$ {
? ? ? proxy_pass http://zcy_project_manage;
? ? ? proxy_set_header? ? ? ? X-Real-IP $remote_addr;
? ? ? proxy_set_header? ? ? ? X-Forwarded-For $proxy_add_x_forwarded_for;
? ? ? proxy_set_header? ? ? ? Host $http_host;
? ? ? add_header Cache-Control 'private, no-store, no-cache, must-revalidate';
? ? }
? ? location /assets_bidding-open/ {
? ? ? proxy_pass http://zcy_bidding-open;
? ? ? proxy_set_header? ? ? ? X-Real-IP $remote_addr;
? ? ? proxy_set_header? ? ? ? X-Forwarded-For $proxy_add_x_forwarded_for;
? ? ? proxy_set_header? ? ? ? Host $http_host;
? ? ? add_header Cache-Control 'private, no-store, no-cache, must-revalidate';
? ? }
? ? location /bidding-entrust/ {
? ? ? proxy_pass http://zcy_bidding-entrust;
? ? ? proxy_set_header? ? ? ? X-Real-IP $remote_addr;
? ? ? proxy_set_header? ? ? ? X-Forwarded-For $proxy_add_x_forwarded_for;
? ? ? proxy_set_header? ? ? ? Host $http_host;
? ? ? add_header Cache-Control 'private, no-store, no-cache, must-revalidate';
? ? }
? ? location /bidding-entrust-assets/ {
? ? ? proxy_pass http://zcy_bidding-entrust;
? ? ? proxy_set_header? ? ? ? X-Real-IP $remote_addr;
? ? ? proxy_set_header? ? ? ? X-Forwarded-For $proxy_add_x_forwarded_for;
? ? ? proxy_set_header? ? ? ? Host $http_host;
? ? ? add_header Cache-Control 'private, no-store, no-cache, must-revalidate';
? ? }
? ? location / {
? ? ? proxy_pass http://zcy_bidding;
? ? ? proxy_set_header? ? ? ? X-Real-IP $remote_addr;
? ? ? proxy_set_header? ? ? ? X-Forwarded-For $proxy_add_x_forwarded_for;
? ? ? proxy_set_header? ? ? ? Host $http_host;
? ? }
}