Nginx+Windows搭建域名訪問(wèn)環(huán)境的操作方法
一、修改 Windows hosts 文件
位置:C:\Windows\System32\drivers\etc
在后面追加以下內(nèi)容:
# guli mall # 192.168.163.131 gulimall.com
二、Nginx 配置文件
三、分析Nginx配置文件
cat /mydata/nginx/conf/nginx.conf
user nginx; worker_processes 1; error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid; events { worker_connections 1024; } http { 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; sendfile on; #tcp_nopush on; keepalive_timeout 65; #gzip on; include /etc/nginx/conf.d/*.conf;
可以看到,在 http 塊中最后有 include /etc/nginx/conf.d/*.conf; 這句配置說(shuō)明在 conf.d 目錄下所有 .conf 后綴的文件內(nèi)容都會(huì)作為 nginx 配置文件 http 塊中的配置。這是為了防止主配置文件太復(fù)雜,也可以對(duì)不同的配置進(jìn)行分類。
下面我們參考 conf.d 目錄下的配置,來(lái)配置 gulimall 的 server 塊配置
四、gulimall.conf
默認(rèn)配置下,我們?cè)L問(wèn) gulimall.com 會(huì)請(qǐng)求 nginx 默認(rèn)的 index 頁(yè)面,現(xiàn)在我們要做的是當(dāng)訪問(wèn) gulimall.com 的時(shí)候轉(zhuǎn)發(fā)到我們的商品模塊的商城首頁(yè)界面。
4.1 查看Windows ip
打開cmd 輸入 ipconfig
這里的 192.168.17.1 和 192.168.163.1 也是 Windows 的本機(jī)地址
所以我們配置當(dāng)訪問(wèn) nginx /請(qǐng)求時(shí)代理到 192.168.163.1:10000 商品服務(wù)首頁(yè)
4.2 配置代理
server { listen 80; server_name gulimall.com; #charset koi8-r; #access_log /var/log/nginx/log/host.access.log main; location / { proxy_pass http://192.168.163.1:10000; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; }
五、圖示
六、反向代理:nginx 代理網(wǎng)關(guān)由網(wǎng)關(guān)進(jìn)行轉(zhuǎn)發(fā)
6.1 修改 nginx.conf
vim /mydata/nginx/conf/nginx.conf
修改 http 塊,配置上游服務(wù)器為網(wǎng)關(guān)地址
user nginx; worker_processes 1; error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid; events { worker_connections 1024; } http { 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; sendfile on; #tcp_nopush on; keepalive_timeout 65; #gzip on; upstream gulimall { server 192.168.163.1:88; } include /etc/nginx/conf.d/*.conf;
6.2 修改 gulimall.conf
配置代理地址為上面配置的上游服務(wù)器名
server { listen 80; server_name gulimall.com; #charset koi8-r; #access_log /var/log/nginx/log/host.access.log main; location / { proxy_set_header Host $host; proxy_pass http://gulimall; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; }
七、訪問(wèn)跳轉(zhuǎn)分析
當(dāng)前通過(guò)域名的方式,請(qǐng)求 gulimal.com ;
根據(jù) hosts 文件的配置,請(qǐng)求 gulimall.com 域名時(shí)會(huì)請(qǐng)求虛擬機(jī) ip
192.168.163.131 gulimall.com
當(dāng)請(qǐng)求到 192.168.163.131:80 時(shí),會(huì)被 nginx 轉(zhuǎn)發(fā)到我們配置的 192.168.163.1:10000 路徑,該路徑為運(yùn)行商品服務(wù)的 windows 主機(jī) ip 地址,至此達(dá)到通過(guò)域名訪問(wèn)商品服務(wù)的目的。
server { listen 80; server_name gulimall.com; location / { proxy_pass http://192.168.163.1:10000; } }
7.1 后面的跳轉(zhuǎn)分析
之后為了統(tǒng)一管理我們的各種服務(wù),我們將通過(guò)配置網(wǎng)關(guān)作為 nginx 轉(zhuǎn)發(fā)的目標(biāo)。最后通過(guò)配置網(wǎng)關(guān)根據(jù)不同的域名來(lái)判斷跳轉(zhuǎn)對(duì)應(yīng)的服務(wù)。
到此這篇關(guān)于Nginx搭建域名訪問(wèn)環(huán)境的文章就介紹到這了,更多相關(guān)Nginx搭建域名訪問(wèn)環(huán)境內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Nginx?ingress?controller高可用的實(shí)現(xiàn)
本文主要介紹了Nginx?ingress?controller高可用的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-06-06利用nginx與ffmpeg搭建流媒體服務(wù)器過(guò)程詳解
這篇文章主要給大家介紹了利用nginx與ffmpeg搭建流媒體服務(wù)器的全過(guò)程,文中介紹的很詳細(xì),對(duì)大家具有一定的參考價(jià)值,需要的朋友們下面來(lái)一起看看吧。2017-03-03Nginx 反向代理緩存(proxy_cache)的實(shí)現(xiàn)
Nginx的緩存加速功能是由proxy_cache和fastcgi_cache兩個(gè)功能模塊完成,本文主要介紹了Nginx 反向代理緩存(proxy_cache)的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2024-05-05關(guān)于nginx報(bào)錯(cuò)405?not?allowed解決方法總結(jié)
這篇文章主要給大家介紹了關(guān)于nginx報(bào)錯(cuò)405?not?allowed解決方法的相關(guān)資料,nginx遇到post請(qǐng)求靜態(tài)文件會(huì)得到405錯(cuò)誤,文中通過(guò)代碼介紹的非常詳細(xì),也給出了推薦方法,需要的朋友可以參考下2023-10-10Nginx+Keepalived實(shí)現(xiàn)雙機(jī)主備的方法
這篇文章主要介紹了Nginx+Keepalived實(shí)現(xiàn)雙機(jī)主備的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03WinPC搭建nginx服務(wù)器的實(shí)現(xiàn)步驟
本文主要介紹了WinPC搭建nginx服務(wù)器的實(shí)現(xiàn)步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01nginx添加nginx-sticky-module模塊步驟的實(shí)現(xiàn)
nginx-sticky-module模塊是nginx實(shí)現(xiàn)負(fù)載均衡的一種方案,和ip_hash負(fù)載均衡算法會(huì)有區(qū)別的,本文主要介紹了nginx添加nginx-sticky-module模塊步驟的實(shí)現(xiàn),感興趣的可以了解一下2023-08-08nginx配置ssl實(shí)現(xiàn)https訪問(wèn)(小白文)
安全起見,需要將之前的http接口訪問(wèn)變成https訪問(wèn),所以需要配置SSL證書,本文主要介紹了nginx配置ssl實(shí)現(xiàn)https訪問(wèn),具有一定的參考價(jià)值,感興趣的可以了解一下2023-09-09