Nginx+Windows搭建域名訪問環(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; 這句配置說明在 conf.d 目錄下所有 .conf 后綴的文件內(nèi)容都會作為 nginx 配置文件 http 塊中的配置。這是為了防止主配置文件太復(fù)雜,也可以對不同的配置進行分類。
下面我們參考 conf.d 目錄下的配置,來配置 gulimall 的 server 塊配置
四、gulimall.conf
默認(rèn)配置下,我們訪問 gulimall.com 會請求 nginx 默認(rèn)的 index 頁面,現(xiàn)在我們要做的是當(dāng)訪問 gulimall.com 的時候轉(zhuǎn)發(fā)到我們的商品模塊的商城首頁界面。
4.1 查看Windows ip
打開cmd 輸入 ipconfig

這里的 192.168.17.1 和 192.168.163.1 也是 Windows 的本機地址
所以我們配置當(dāng)訪問 nginx /請求時代理到 192.168.163.1:10000 商品服務(wù)首頁
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)進行轉(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;
}
七、訪問跳轉(zhuǎn)分析
當(dāng)前通過域名的方式,請求 gulimal.com ;
根據(jù) hosts 文件的配置,請求 gulimall.com 域名時會請求虛擬機 ip
192.168.163.131 gulimall.com
當(dāng)請求到 192.168.163.131:80 時,會被 nginx 轉(zhuǎn)發(fā)到我們配置的 192.168.163.1:10000 路徑,該路徑為運行商品服務(wù)的 windows 主機 ip 地址,至此達到通過域名訪問商品服務(wù)的目的。
server {
listen 80;
server_name gulimall.com;
location / {
proxy_pass http://192.168.163.1:10000;
}
}7.1 后面的跳轉(zhuǎn)分析
之后為了統(tǒng)一管理我們的各種服務(wù),我們將通過配置網(wǎng)關(guān)作為 nginx 轉(zhuǎn)發(fā)的目標(biāo)。最后通過配置網(wǎng)關(guān)根據(jù)不同的域名來判斷跳轉(zhuǎn)對應(yīng)的服務(wù)。

到此這篇關(guān)于Nginx搭建域名訪問環(huán)境的文章就介紹到這了,更多相關(guān)Nginx搭建域名訪問環(huán)境內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
采用ngxtop實現(xiàn)nginx實時訪問數(shù)據(jù)統(tǒng)計
這篇文章主要介紹了采用ngxtop實現(xiàn)nginx實時訪問數(shù)據(jù)統(tǒng)計,需要的朋友可以參考下2014-07-07
nginx實現(xiàn)根據(jù)URL轉(zhuǎn)發(fā)請求的實戰(zhàn)經(jīng)歷
這篇文章主要給大家介紹了一次關(guān)于nginx實現(xiàn)根據(jù)URL轉(zhuǎn)發(fā)請求的實戰(zhàn)經(jīng)歷,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用nginx具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11
Nginx正向代理實現(xiàn)局域網(wǎng)電腦訪問外網(wǎng)的過程詳解
在工作中我遇到了一個類似的情況:在公司網(wǎng)絡(luò)中,由于管理要求,局域網(wǎng)內(nèi)的電腦不能直接訪問外網(wǎng),但是,工作上領(lǐng)導(dǎo)吩咐需要讓局域網(wǎng)內(nèi)的電腦能夠訪問外網(wǎng)上的某個網(wǎng)站,這時候就需要用到正向代理,本文將介紹如何配置 Nginx 實現(xiàn)這一功能,需要的朋友可以參考下2024-03-03

