Mac上Nginx學(xué)習(xí)筆記

Nginx概念:反向代理(不關(guān)心服務(wù)器是誰(shuí))服務(wù)器

一、Nginx常用命令:

關(guān)閉nginx:nginx -s stop/quit? ?

stop是快速關(guān)閉,quit正常關(guān)閉(等請(qǐng)求走完才關(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不一致的問(wèn)題

????????????4)fair(第三方),響應(yīng)時(shí)間越短的分配訪問(wèn)次數(shù)越多

? ? ? ? ? b、server塊:

????????????location方式:

????????????????1)=? 精確匹配

????????????????2)~? 正則匹配,區(qū)分大小寫

????????????????3)~* 正則匹配,不區(qū)分大小寫

????????????????4)^~ 在前綴匹配中,視作最長(zhǎng)長(zhǎng)度匹配

????????????匹配規(guī)則:

????????????????1)先查找 = 精確匹配,找到了就返回該條

????????????????2)查找最長(zhǎng)前綴匹配^~,匹配到了就返回該條

????????????????3)沒有精確匹配,和最長(zhǎng)前綴匹配,“按順序”查找正則匹配,~,~*兩種,找到任何一條匹配正則就返回

????????????????4)上面三者都沒,等于用普通前綴長(zhǎng)度匹配規(guī)則,記錄下最長(zhǎng)匹配的那條返回

????????????????5)/ 匹配全部,等于是兜底的最低級(jí)的前綴匹配



三、配置文件例子和注釋


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;

? ? }

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容