服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

云服务器|WEB服务器|FTP服务器|邮件服务器|虚拟主机|服务器安全|DNS服务器|服务器知识|Nginx|IIS|Tomcat|

服务器之家 - 服务器技术 - Nginx - Nginx+Windows搭建域名访问环境的操作方法

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

2022-07-29 09:31随遇而安== Nginx

这篇文章主要介绍了Nginx搭建域名访问环境,包括nginx配置文件的相关介绍及对nginx配置文件的分析,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

一、修改 windows hosts 文件

位置:c:\windows\system32\drivers\etc

在后面追加以下内容:

?
1
2
# guli mall #
192.168.163.131     gulimall.com

二、nginx 配置文件

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

三、分析nginx配置文件

?
1
cat /mydata/nginx/conf/nginx.conf
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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 配置代理

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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

?
1
vim /mydata/nginx/conf/nginx.conf

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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

?
1
192.168.163.131     gulimall.com

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

?
1
2
3
4
5
6
7
server {
    listen       80;
    server_name  gulimall.com;
    location / {
      proxy_pass http://192.168.163.1:10000;
    }
}

7.1 后面的跳转分析

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

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

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

原文链接:https://www.cnblogs.com/55zjc/p/16016030.html

延伸 · 阅读

精彩推荐
  • Nginx前端开发必须了解的 Nginx 单页加载优化

    前端开发必须了解的 Nginx 单页加载优化

    本文介绍从一个前端的角度简单介绍一下Nginx中页面加载的优化工作。一起来看看吧。...

    前端大全3982022-01-05
  • NginxNginx缓存设置案例详解

    Nginx缓存设置案例详解

    这篇文章主要介绍了Nginx缓存设置案例详解,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下...

    多纤果冻6942021-09-27
  • NginxNGINX服务器配置404错误页面转向的方法

    NGINX服务器配置404错误页面转向的方法

    这篇文章主要为大家详细介绍了NGINX服务器配置404错误页面转向的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下 ...

    php架构师5492019-12-11
  • Nginx详解基于centos7搭建Nginx网站服务器(包含虚拟web主机的配置)

    详解基于centos7搭建Nginx网站服务器(包含虚拟web主机的配置)

    这篇文章主要介绍了详解基于centos7搭建Nginx网站服务器(包含虚拟web主机的配置),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的...

    mb5c7bb57d7f7e86132020-01-20
  • NginxNginx反斜杠自动解码问题解决方法

    Nginx反斜杠自动解码问题解决方法

    这篇文章主要介绍了Nginx反斜杠自动解码问题解决方法,有些时候是不需要解码的,可nginx缺会自动解码,本文讲解的就是如何解决这个问题,需要的朋友可以参...

    Nginx技术网6202019-10-26
  • NginxNginx一个域名访问多个项目的方法实例

    Nginx一个域名访问多个项目的方法实例

    这篇文章主要给大家介绍了关于Nginx一个域名访问多个项目的方法,文中通过示例代码介绍的非常详细,对大家学习或者使用Nginx具有一定的参考学习价值,...

    daisy4232020-03-12
  • NginxNginx 根据URL带的参数转发的实现

    Nginx 根据URL带的参数转发的实现

    这篇文章主要介绍了Nginx 根据URL带的参数转发的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们...

    牧之山野16912020-09-27
  • NginxNginx中Location从零开始的配置教程

    Nginx中Location从零开始的配置教程

    这篇文章主要给大家介绍了关于Nginx中Location从零开始的配置教程,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需...

    市民X3482019-12-29