Nginx+Windows搭建域名访问环境的操作方法


Posted in Servers onMarch 17, 2022

一、修改 Windows hosts 文件

位置:C:\Windows\System32\drivers\etc

在后面追加以下内容:

# guli mall #
192.168.163.131		gulimall.com

二、Nginx 配置文件

Nginx+Windows搭建域名访问环境的操作方法

三、分析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 后缀的文件内容都会作为 nginx 配置文件 http 块中的配置。这是为了防止主配置文件太复杂,也可以对不同的配置进行分类。

下面我们参考 conf.d 目录下的配置,来配置 gulimall 的 server 块配置

四、gulimall.conf

默认配置下,我们访问 gulimall.com 会请求 nginx 默认的 index 页面,现在我们要做的是当访问 gulimall.com 的时候转发到我们的商品模块的商城首页界面。

4.1 查看Windows ip

打开cmd 输入 ipconfig

Nginx+Windows搭建域名访问环境的操作方法

这里的 192.168.17.1 和 192.168.163.1 也是 Windows 的本机地址

所以我们配置当访问 nginx /请求时代理到 192.168.163.1:10000 商品服务首页

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+Windows搭建域名访问环境的操作方法

六、反向代理:nginx 代理网关由网关进行转发

6.1 修改 nginx.conf

vim /mydata/nginx/conf/nginx.conf

修改 http 块,配置上游服务器为网关地址

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

配置代理地址为上面配置的上游服务器名

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;
}

Nginx+Windows搭建域名访问环境的操作方法

 

七、访问跳转分析

当前通过域名的方式,请求 gulimal.com ;

根据 hosts 文件的配置,请求 gulimall.com 域名时会请求虚拟机 ip

192.168.163.131		gulimall.com

当请求到 192.168.163.131:80 时,会被 nginx 转发到我们配置的 192.168.163.1:10000 路径,该路径为运行商品服务的 windows 主机 ip 地址,至此达到通过域名访问商品服务的目的。

server {
    listen       80;
    server_name  gulimall.com;
    location / {
      proxy_pass http://192.168.163.1:10000;
    }
}

7.1 后面的跳转分析

之后为了统一管理我们的各种服务,我们将通过配置网关作为 nginx 转发的目标。最后通过配置网关根据不同的域名来判断跳转对应的服务。

Nginx+Windows搭建域名访问环境的操作方法

到此这篇关于Nginx搭建域名访问环境的文章就介绍到这了,更多相关Nginx搭建域名访问环境内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Servers 相关文章推荐
Nginx访问日志及错误日志参数说明
Mar 31 Servers
Nginx搭建rtmp直播服务器实现代码
Mar 31 Servers
详解nginx.conf 中 root 目录设置问题
Apr 01 Servers
Apache POI的基本使用详解
Nov 07 Servers
详解使用内网穿透工具Ngrok代理本地服务
Mar 31 Servers
Nginx反向代理、重定向
Apr 13 Servers
nginx日志格式分析和修改
Apr 28 Servers
如何开启Apache,Nginx和IIS服务器的GZIP压缩功能
Apr 29 Servers
如何Tomcat中使用ipv6地址
May 06 Servers
nginx rewrite功能使用场景分析
May 30 Servers
git stash(储藏)的用法总结
Jun 25 Servers
Shell中的单中括号和双中括号的用法详解
Dec 24 Servers
nginx中封禁ip和允许内网ip访问的实现示例
Mar 17 #Servers
Apache Linkis 中间件架构及快速安装步骤
关于Nginx中虚拟主机的一些冷门知识小结
Mar 03 #Servers
nginx负载功能+nfs服务器功能解析
Apache Pulsar集群搭建部署详细过程
Feb 12 #Servers
详解nginx安装过程并代理下载服务器文件
Feb 12 #Servers
nginx从安装到配置详细说明(安装,安全配置,防盗链,动静分离,配置 HTTPS,性能优化)
You might like
解析PHP对现有搜索引擎的调用
2013/06/25 PHP
php中in_array函数用法分析
2014/11/15 PHP
php使用cookie实现记住用户名和密码实现代码
2015/04/27 PHP
ajax+php实现无刷新验证手机号的实例
2017/12/22 PHP
使用laravel的Eloquent模型如何获取数据库的指定列
2019/10/17 PHP
javascript中比较字符串是否相等的方法
2013/07/23 Javascript
提高jQuery性能的十个诀窍
2013/11/14 Javascript
nodejs批量修改文件编码格式
2015/01/22 NodeJs
js实现绿白相间竖向网页百叶窗动画切换效果
2015/03/02 Javascript
js实现一个可以兼容PC端和移动端的div拖动效果实例
2016/12/09 Javascript
JavaScript实现简单的四则运算计算器完整实例
2017/04/28 Javascript
H5+C3+JS实现五子棋游戏(AI篇)
2020/05/28 Javascript
基于axios 的responseType类型的设置方法
2019/10/29 Javascript
[02:27]刀塔重生降临
2015/10/14 DOTA
[06:07]DOTA2-DPC中国联赛3月5日Recap集锦
2021/03/11 DOTA
Python内存管理方式和垃圾回收算法解析
2017/11/11 Python
浅谈Python使用Bottle来提供一个简单的web服务
2017/12/27 Python
python抽取指定url页面的title方法
2018/05/11 Python
详解Python if-elif-else知识点
2018/06/11 Python
python 2.7 检测一个网页是否能正常访问的方法
2018/12/26 Python
Python 安装第三方库 pip install 安装慢安装不上的解决办法
2019/06/18 Python
PyQT5 QTableView显示绑定数据的实例详解
2019/06/25 Python
python打包成so文件过程解析
2019/09/28 Python
有关pycharm登录github时有的时候会报错connection reset的问题
2020/09/15 Python
python3.7中安装paddleocr及paddlepaddle包的多种方法
2020/11/27 Python
请解释接口的显式实现有什么意义
2012/05/26 面试题
团工委书记自荐书范文
2013/12/17 职场文书
餐饮部总监岗位职责范文
2014/02/13 职场文书
优秀乡村医生事迹材料
2014/05/28 职场文书
2014年学雷锋活动总结
2014/06/26 职场文书
毕业证丢失证明范本
2014/09/20 职场文书
2014年培训工作总结范文
2014/11/27 职场文书
领导离职感言
2015/08/03 职场文书
2016清明节森林防火广播稿
2015/12/17 职场文书
golang 实现Location跳转方式
2021/05/02 Golang
Python基础之hashlib模块详解
2021/05/06 Python