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 反向代理之 proxy_pass的实现
Mar 31 Servers
nginx中proxy_pass各种用法详解
Nov 07 Servers
教你快速构建一个基于nginx的web集群项目
Nov 27 Servers
nginx刷新页面出现404解决方案(亲测有效)
Mar 18 Servers
HDFS免重启挂载新磁盘
Apr 06 Servers
CentOS下安装Jenkins的完整步骤
Apr 07 Servers
tomcat正常启动但网页却无法访问的几种解决方法
May 06 Servers
排查Tomcat进程假死的问题
May 06 Servers
Python安装及建立虚拟环境的完整步骤
Jun 25 Servers
教你nginx跳转配置的四种方式
Jul 07 Servers
本地搭建minio文件服务器(使用bat脚本启动)的方法
Jul 15 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 file_exists 检查文件或目录是否存在的函数
2010/05/10 PHP
PHP 解决session死锁的方法
2013/06/20 PHP
PHP实现通过URL提取根域名
2016/03/31 PHP
Yii的Srbac插件用法详解
2016/07/14 PHP
Yii2框架实现注册和登录教程
2016/09/30 PHP
tp5.1 框架数据库常见操作详解【添加、删除、更新、查询】
2020/05/26 PHP
基于jquery的可多选的下拉列表框
2012/07/20 Javascript
jQuery获取选中内容及设置元素属性的方法
2014/07/09 Javascript
JavaScript中双叹号!!作用示例介绍
2014/09/21 Javascript
jQuery实现个性翻牌效果导航菜单的方法
2015/03/09 Javascript
手机开发必备技巧:javascript及CSS功能代码分享
2015/05/25 Javascript
jquery任意位置浮动固定层插件用法实例
2015/05/29 Javascript
浅谈jQuery 选择器和dom操作
2016/06/07 Javascript
CentOS 安装NodeJS V8.0.0的方法
2017/06/15 NodeJs
VUE element-ui 写个复用Table组件的示例代码
2017/11/18 Javascript
vue中手机号,邮箱正则验证以及60s发送验证码的实例
2018/03/16 Javascript
electron + vue项目实现打印小票功能及实现代码
2018/11/25 Javascript
微信小程序实现自定义动画弹框/提示框的方法实例
2020/11/06 Javascript
JavaScript缓动动画函数的封装方法
2020/11/25 Javascript
Python 的 Socket 编程
2015/03/24 Python
Python urllib、urllib2、httplib抓取网页代码实例
2015/05/09 Python
Python中格式化format()方法详解
2017/04/01 Python
pandas带有重复索引操作方法
2018/06/08 Python
python3判断url链接是否为404的方法
2018/08/10 Python
详解Django将秒转换为xx天xx时xx分
2019/09/27 Python
opencv python如何实现图像二值化
2020/02/03 Python
Python发送手机动态验证码代码实例
2020/02/28 Python
Python 在 VSCode 中使用 IPython Kernel 的方法详解
2020/09/05 Python
实例讲解CSS3中的box-flex弹性盒属性布局
2016/06/09 HTML / CSS
美国眼镜网:GlassesUSA
2017/09/07 全球购物
LN-CC中国:高端男装和女装的奢侈时尚目的地
2019/09/14 全球购物
全球工业:Global Industrial
2020/02/01 全球购物
Shopbop中文官网:美国亚马逊旗下时尚购物网站
2020/12/15 全球购物
如何删除一个表里面的重复行
2013/07/13 面试题
如何判断pytorch是否支持GPU加速
2021/06/01 Python
聊聊Lombok中的@Builder注解使用教程
2021/11/17 Java/Android