焦點資訊:Nginx 常用的基礎(chǔ)配置(web前端相關(guān)方面)
(相關(guān)資料圖)
最近很多朋友通過趣站網(wǎng)問到Nginx配置前端 web 服務(wù)
,所以特地寫了這篇文章;希望能夠幫助更多的朋友。
基礎(chǔ)配置
user root;worker_processes 1;events { worker_connections 10240;}http { log_format "$remote_addr - $remote_user [$time_local] " ""$request" $status $body_bytes_sent " ""$http_referer" "$http_user_agent""; include mime.types; default_type application/octet-stream; sendfile on; #autoindex on; #autoindex_exact_size off; autoindex_localtime on; keepalive_timeout 65; gzip on; gzip_disable "msie6"; gzip_min_length 100; gzip_buffers 4 16k; gzip_comp_level 1; gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png; gzip_types "*"; gzip_vary off; server_tokens off; client_max_body_size 200m; server { listen 80 default_server; server_name _; return 403 /www/403/index.html; } include ../serve/*.conf;}復(fù)制代碼
隱藏 Nginx 版本信息
http { server_tokens off;}復(fù)制代碼
禁止ip直接訪問80端口
server { listen 80 default; server_name _; return 500;}復(fù)制代碼
啟動 web 服務(wù) (vue 項目為例)
server { # 項目啟動端口 listen 80; # 域名(localhost) server_name _; # 禁止 iframe 嵌套 add_header X-Frame-Options SAMEORIGIN; # 訪問地址 根路徑配置 location / { # 項目目錄 root html; # 默認(rèn)讀取文件 index index.html; # 配置 history 模式的刷新空白 try_files $uri $uri/ /index.html; } # 后綴匹配 ,解決靜態(tài)資源找不到問題 location ~* \.(gif|jpg|jpeg|png|css|js|ico)$ { root html/static/; } # 圖片防盜鏈 location ~/static/.*\.(jpg|jpeg|png|gif|webp)$ { root html; valid_referers *.deeruby.com; if ($invalid_referer) { return 403; } } # 訪問限制 location /static { root html; # allow 允許 allow 39.xxx.xxx.xxx; # deny 拒絕 deny all; }}復(fù)制代碼PC端和移動端使用不同的項目文件映射
server { ...... location / { root /home/static/pc; if ($http_user_agent ~* "(mobile|android|iphone|ipod|phone)") { root /home/static/mobile; } index index.html; }}復(fù)制代碼
一個web服務(wù),配置多個項目 (location 匹配路由區(qū)別)
server { listen 80; server_name _; # 主應(yīng)用 location / { root html/main; index index.html; try_files $uri $uri/ /index.html; } # 子應(yīng)用一 location ^~ /store/ { proxy_pass http://localhost:8001; proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } # 子應(yīng)用二 location ^~ /school/ { proxy_pass http://localhost:8002; proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } # 靜態(tài)資源讀取不到問題處理 rewrite ^/api/profile/(.*)$ /(替換成正確路徑的文件的上一層目錄)/$1 last;}# 子應(yīng)用一服務(wù)server { listen 8001; server_name _; location / { root html/store; index index.html; try_files $uri $uri/ /index.html; } location ^~ /store/ { alias html/store/; index index.html index.htm; try_files $uri /store/index.html; } # 接口代理 location /api { proxy_pass http://localhost:8089; }}# 子應(yīng)用二服務(wù)server { listen 8002; server_name _; location / { root html/school; index index.html; try_files $uri $uri/ /index.html; } location ^~ /school/ { alias html/school/; index index.html index.htm; try_files $uri /school/index.html; } # 接口代理 location /api { proxy_pass http://localhost:10010; }}復(fù)制代碼
配置負(fù)載均衡
upstream my_upstream { server http://localhost:9001; server http://localhost:9002; server http://localhost:9003;}server { listen 9000; server_name test.com; location / { proxy_pass my_upstream; proxy_set_header Host $proxy_host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; }}復(fù)制代碼
SSL 配置 HTTPS
server { listen 80; server_name www.xxx.com; # 將 http 重定向轉(zhuǎn)移到 https return 301 https://$server_name$request_uri;}server { listen 443 ssl; server_name www.xxx.com; ssl_certificate /etc/nginx/ssl/www.xxx.com.pem; ssl_certificate_key /etc/nginx/ssl/www.xxx.com.key; ssl_session_timeout 10m; ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_prefer_server_ciphers on; location / { root /project/xxx; index index.html index.htm index.md; try_files $uri $uri/ /index.html; }}
關(guān)鍵詞:
Nginx
責(zé)任編輯:孫知兵
免責(zé)聲明:本文僅代表作者個人觀點,與太平洋財富網(wǎng)無關(guān)
。其原創(chuàng)性以及文中陳述文字和內(nèi)容未經(jīng)本站證實,對本文以及其中全部或者部分內(nèi)容、文字的真實性、完整性、及時性本站不作任何保證或承諾,請讀者僅作參考,并請自行核實相關(guān)內(nèi)容。如有問題,請聯(lián)系我們!
- 天天快資訊:銀保監(jiān)會:2022年末小微企業(yè)貸2023-02-16
- 世界快消息!山東開元電子有限公司2023-02-16
- 環(huán)球最資訊丨【讀財報】三季度保險公司消費2023-02-16
- 焦點關(guān)注:通策醫(yī)療終止收購和仁科技股份 2023-02-16
- 上市航司1月份經(jīng)營實現(xiàn)開門紅 “航空超級2023-02-16
- 天天微頭條丨ST天業(yè):楊超訴公司證券虛假陳2023-02-16
- 【天天快播報】股票行情在線看 A股收市432023-02-16
- 如何買股票新手入門?|如何在最佳時機買入2023-02-16
- 【世界獨家】防己科是什么?本科的粉防己、2023-02-16
- 世界球精選!最新!央行調(diào)整職工住房公積金2023-02-16
- 全球今頭條!廣西桂花樹有什么特征?廣西桂2023-02-16
- 商業(yè)城半年報:凈利大增2.2億 轉(zhuǎn)讓盛京銀2023-02-16
- 環(huán)球即時:仙丹花怎么養(yǎng)?仙丹花的形態(tài)特征2023-02-16
- 滬深300指數(shù)基金推薦:132支中證500基金(上)2023-02-16
- 天天觀點:農(nóng)村黃瓜的主要病蟲害是什么
?如2023-02-16- 世界信息:首套房房貸利率下調(diào)至4.25% 樓市2023-02-16
- 【環(huán)球快播報】為什么要炸北溪管道?北溪22023-02-16
- 當(dāng)前速看:雪美人的栽培技術(shù)你知道嗎
?雪美2023-02-16- 環(huán)球即時看
!環(huán)球黑卡究竟是什么?持卡人可2023-02-16- 【天天報資訊】怎么和銀行談停息掛賬
?停息2023-02-16- 全球報道:腰果的價格區(qū)間是多少
?如何確保2023-02-16- 【世界聚看點】海富通基金管理有限公司:102023-02-16
- 每日速遞:宋引章喜歡顧千帆嗎?最后的結(jié)局2023-02-16
- 環(huán)球微速訊:蚜蟲怎么治療
?三角梅殺死蚜蟲2023-02-16- 京東金融推出“8.8%”理財產(chǎn)品 基金面值將2023-02-16
- 股票分紅為何要除權(quán)
?配股是否需要除權(quán)除息2023-02-16- 觀察:千針萬線草是什么
?千針萬線草有什么2023-02-16- 實時:互聯(lián)網(wǎng)金融龍頭股票 五只低估值極具2023-02-16
- 豌豆苗的種植方法 種植豌豆苗要注意的幾種2023-02-16
- 富士康遭黑客勒索比特幣1804枚 價格高達(dá)2.2023-02-16
精彩推薦
- 天天快資訊:銀保監(jiān)會:2022年末小微企...
- 世界快消息!山東開元電子有限公司
- ?北溪2號永久報廢了嗎?">【環(huán)球快播報】為什么要炸北溪管道
?北...- ?停息掛賬申請的條件">【天天報資訊】怎么和銀行談停息掛賬?...
- ?最后的結(jié)局誰在一起
?">每日速遞:宋引章喜歡顧千帆嗎?最后的...- ?配股是否需要除權(quán)除息?">焦點速訊:楊冪胡歌翻臉原因 楊冪胡歌...
- ?金巧巧結(jié)過幾次婚姻?">博納影業(yè)總裁于冬身價多少億
?金巧巧結(jié)...- 英國留學(xué)生包機回國費用公布:經(jīng)濟(jì)艙票...
閱讀排行
- 環(huán)球最資訊丨【讀財報】三季度保...
- 焦點關(guān)注:通策醫(yī)療終止收購和仁...
- 上市航司1月份經(jīng)營實現(xiàn)開門紅 ...
- !溫商陳崇軍鎖定鈣鈦礦電池">全球最新:萬物皆可光伏!溫商陳...
- !涉及這家上市公司">批捕!涉及這家上市公司
- 零跑C11增程正式開啟預(yù)售
- ,公司正在積極推進(jìn)REITs項目(附調(diào)研問答)">熱點
!招商公路獲8家機構(gòu)調(diào)研:...- 環(huán)球速看:龍佰集團(tuán)獲13家機構(gòu)調(diào)...
- 當(dāng)前快看:黃光裕真假離場 “國...
- 世界聚焦:公司前線|揚州金泉新...
-
,助你新年大展鴻“兔”!">
!" />
,助你新年大展鴻“兔”!">泰康大健康事業(yè)合伙人英才計劃
,助你新年大展鴻“兔”關(guān)于我們 - 聯(lián)系方式 - 版權(quán)聲明 - 招聘信息 - 友鏈交換 - 網(wǎng)站統(tǒng)計
太平洋財富主辦 版權(quán)所有:太平洋財富網(wǎng)
?
中國互聯(lián)網(wǎng)違法和不良信息舉報中心
Copyright© 2012-2020 太平洋財富網(wǎng)(m.rkrking.com) All rights reserved.
未經(jīng)過本站允許 請勿將本站內(nèi)容傳播或復(fù)制 業(yè)務(wù)QQ:3 31 986 683
- ?北溪2號永久報廢了嗎?">【環(huán)球快播報】為什么要炸北溪管道