日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

Nginx+Windows搭建域名訪問環境的操作方法_nginx

作者:隨遇而安== ? 更新時間: 2022-05-19 編程語言

一、修改 Windows hosts 文件

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

在后面追加以下內容:

# guli mall #
192.168.163.131		gulimall.com

二、Nginx 配置文件

三、分析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

這里的 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 代理網關由網關進行轉發

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

?

七、訪問跳轉分析

當前通過域名的方式,請求 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 轉發的目標。最后通過配置網關根據不同的域名來判斷跳轉對應的服務。

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

欄目分類
最近更新