• nginx搭建域名访问环境(反向代理配置)(练习用)


    前提:已经在虚拟机中配置好nginx(docker),有可访问的项目

    1.找到host文件所在位置:

    C:\Windows\System32\drivers\etc

    进行修改,加入你想要的域名和你的虚拟机地址,比如:

    xs.com 192.168.56.10

    接着访问xs.com:9200(这里可以是随便一个服务,比如nacos),正常访问既是设置成功

    或者直接访问xs.com,出现了nginx相关信息即代表设置成功

    2.进入挂载在外的nginx文件,

    其中conf.d中的default.conf是conf文件夹下的nginx.conf的子文件,无论有什么内容都会包含进去。

    [root@localhost ~]# cd /mydata/nginx/conf/conf.d/

    [root@localhost conf.d]# cat default.conf 
    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;
        #access_log  /var/log/nginx/log/host.access.log  main;

        location / {
            root   /usr/share/nginx/html;
            index  index.html index.htm;
        }

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

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }

    3.复制一份新的配置文件

    [root@localhost conf.d]# cp default.conf xs.conf

    [root@localhost conf.d]# vi xs.conf

    修改以下内容:

    server_name  xs.com;

    然后回到windows使用ipconig命令得到虚拟机的ipv4地址,比如我的就是192.168.56.1

    回到xs.conf,修改得到以下内容:

    location / {
            proxy_pass http://192.168.56.1:9001;
        }
    其中9001是你的项目模块的端口

    保存退出,重启nginx

    [root@localhost conf.d]# docker restart nginx

    windows访问xs.com,转到项目设置的默认页即算成功

    4.配置网关负载均衡

    重新回到上级的nginx.conf,在“include /etc/nginx/conf.d/*.conf;”上面配置上游服务:

    [root@localhost conf.d]# cd ../

    [root@localhost conf]# vi nginx.conf
    upstream xs{
        server 192.168.56.1:88;
    }

    我使用88端口是因为我的项目网关配置的是88端口。
    再回到xs.conf

    [root@localhost conf]# cd conf.d
    [root@localhost conf.d]# vi xs.conf
    修改得到以下内容:

    location / {

            proxy_set_header Host $host;
            proxy_pass http://xs;
        }
    保存退出。

    5.回到项目网关进行配置,我的是springcloud gateway。

    在applicaiton.yml中加入以下配置:

    - id: xs_host_route
      uri: lb://xs
      predicates:
        - Host=**.xs.com

    其中xs就是你要负载均衡的模块的名字

    再次访问xs.com,转到项目设置的默认页即算成功

  • 相关阅读:
    使用HTML制作静态网站作业——我的校园运动会(HTML+CSS)
    JVM(5)面试篇
    5分钟了解Redis的内部实现快速列表(quicklist)
    学习总结:jQuery插件——Validation Plugin
    Oracle数据库接口用户创建
    flutter是什么
    LLMs 驱动的数据合成、整理和评估
    维修派单系统好用吗?如何实现数字化后勤管理?
    MySQL数据库基础知识要点总结
    盲人使用公共设施:科技助力无障碍出行与智能识别
  • 原文地址:https://blog.csdn.net/xushuai2333333/article/details/126489328