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設(shè)置超時(shí)時(shí)間的問(wèn)題及解決方案
程序在處理大量數(shù)據(jù),接口超過(guò)1分鐘(默認(rèn)的)未返回?cái)?shù)據(jù),導(dǎo)致等待超時(shí),出現(xiàn)這種情況,我們可以先優(yōu)化程序,縮短執(zhí)行時(shí)間,可以調(diào)大nginx超時(shí)限制的參數(shù),使程序可以正常執(zhí)行,本文介紹nginx設(shè)置超時(shí)時(shí)間及504 Gateway Time-out的問(wèn)題解決方案,一起看看吧2024-02-02
采用ngxtop實(shí)現(xiàn)nginx實(shí)時(shí)訪問(wèn)數(shù)據(jù)統(tǒng)計(jì)
這篇文章主要介紹了采用ngxtop實(shí)現(xiàn)nginx實(shí)時(shí)訪問(wèn)數(shù)據(jù)統(tǒng)計(jì),需要的朋友可以參考下2014-07-07
nginx實(shí)現(xiàn)根據(jù)URL轉(zhuǎn)發(fā)請(qǐng)求的實(shí)戰(zhàn)經(jīng)歷
這篇文章主要給大家介紹了一次關(guān)于nginx實(shí)現(xiàn)根據(jù)URL轉(zhuǎn)發(fā)請(qǐng)求的實(shí)戰(zhàn)經(jīng)歷,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用nginx具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11
使用nginx搭建點(diǎn)播和直播流媒體服務(wù)器的方法步驟
本篇文章主要介紹了使用nginx搭建點(diǎn)播和直播流媒體服務(wù)器的方法步驟,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-03-03
Nginx正向代理實(shí)現(xiàn)局域網(wǎng)電腦訪問(wèn)外網(wǎng)的過(guò)程詳解
在工作中我遇到了一個(gè)類似的情況:在公司網(wǎng)絡(luò)中,由于管理要求,局域網(wǎng)內(nèi)的電腦不能直接訪問(wèn)外網(wǎng),但是,工作上領(lǐng)導(dǎo)吩咐需要讓局域網(wǎng)內(nèi)的電腦能夠訪問(wèn)外網(wǎng)上的某個(gè)網(wǎng)站,這時(shí)候就需要用到正向代理,本文將介紹如何配置 Nginx 實(shí)現(xiàn)這一功能,需要的朋友可以參考下2024-03-03
解決nginx重新加載配置文件,配置文件沒生效問(wèn)題
這篇文章主要介紹了解決nginx重新加載配置文件,配置文件沒生效問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-06-06

