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实现限制某IP短时间访问次数
Mar 31 Servers
Nginx服务器添加Systemd自定义服务过程解析
Mar 31 Servers
Nginx四层负载均衡的配置指南
Jun 11 Servers
学习nginx基础知识
Sep 04 Servers
Nginx的gzip相关介绍
May 11 Servers
服务器间如何实现文件共享
May 20 Servers
永中文档在线转换预览基于nginx配置部署方案
Jun 10 Servers
Ubuntu安装Mysql+启用远程连接的完整过程
Jun 21 Servers
Win10系统搭建ftp文件服务器详细教程
Aug 05 Servers
解决ubuntu安装软件时,status-code=409报错的问题
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
同时提取多条新闻中的文本一例
2006/10/09 PHP
php获取淘宝分类id示例
2014/01/16 PHP
php抽象类使用要点与注意事项分析
2015/02/09 PHP
php使用文本统计访问量的方法
2016/05/12 PHP
php getcwd与dirname(__FILE__)区别详解
2016/09/24 PHP
PHP convert_uudecode()函数讲解
2019/02/14 PHP
javascript 兼容FF的onmouseenter和onmouseleave的代码
2008/07/19 Javascript
Extjs学习笔记之九 数据模型(上)
2010/01/11 Javascript
Js 冒泡事件阻止实现代码
2013/01/27 Javascript
fckeditor粘贴Word时弹出窗口取消的方法
2014/10/30 Javascript
JS实现完全语义化的网页选项卡效果代码
2015/09/15 Javascript
Jquery 全选反选实例代码
2015/11/19 Javascript
Angular ng-repeat指令实例以及扩展部分
2016/12/26 Javascript
ES6 系列之 WeakMap的使用示例
2018/08/06 Javascript
element-ui表格数据转换的示例代码
2018/08/24 Javascript
node.js使用express框架进行文件上传详解
2019/03/03 Javascript
js简单遍历获取对象中的属性值的方法示例
2019/06/19 Javascript
VUE+elementui组件在table-cell单元格中绘制微型echarts图
2020/04/20 Javascript
JS中的变量作用域(console版)
2020/07/18 Javascript
微信小程序实现分页加载效果
2020/11/19 Javascript
二种python发送邮件实例讲解(python发邮件附件可以使用email模块实现)
2013/12/03 Python
python中的函数用法入门教程
2014/09/02 Python
Python中类的继承代码实例
2014/10/28 Python
Python常见异常分类与处理方法
2017/06/04 Python
Python实现句子翻译功能
2017/11/14 Python
Java编程迭代地删除文件夹及其下的所有文件实例
2018/02/10 Python
python中列表的含义及用法
2020/05/26 Python
python实现计算器简易版
2020/12/17 Python
css3实现文字首尾衔接跑马灯的示例代码
2020/10/16 HTML / CSS
html5+css如何实现中间大两头小的轮播效果
2018/12/06 HTML / CSS
购买澳大利亚最好的服装和内衣在线:BONDS
2016/10/14 全球购物
电子商务应届生求职信
2013/11/16 职场文书
考试作弊检讨书1000字(5篇)
2014/10/19 职场文书
2014年乡镇安全生产工作总结
2014/12/02 职场文书
爱牙日宣传活动总结
2015/02/05 职场文书
GoLang中生成UUID唯一标识的实现
2021/05/08 Golang