侧边栏壁纸
  • 累计撰写 11 篇文章
  • 累计创建 10 个标签
  • 累计收到 3 条评论
隐藏侧边栏

云服务器nginx配置访问谷粒商城

九怀
2022-01-09 / 0 评论 / 0 点赞 / 149 阅读 / 9,423 字 / 正在检测是否收录...
温馨提示:
本文最后更新于 2022-01-20,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。

1.Nginx配置文件

image-20220107221218312

1.1配置docker容器中的nginx服务

  • 随便启动一个nginx实例,只是为了复制出配置

    docker run -p 8081:80 --name nginx -d nginx:1.10   
    
  • 将容器内的配置文件拷贝到/mydata/nginx/conf/ 下

    mkdir -p /mydata/nginx/html
    mkdir -p /mydata/nginx/logs
    mkdir -p /mydata/nginx/conf
    docker container cp nginx:/etc/nginx .  #拷贝到当前文件夹
    #由于拷贝完成后会在config中存在一个nginx文件夹,所以需要将它的内容移动到conf中
    mv /mydata/nginx/conf/nginx/* /mydata/nginx/conf/
    rm -rf /mydata/nginx/conf/nginx
    
  • 终止原容器:

    docker stop nginx
    
  • 执行命令删除原容器:

    docker rm nginx
    
  • 创建新的Nginx,执行以下命令

    docker run -p 8081:80 --name nginx \
     -v /mydata/nginx/html:/usr/share/nginx/html \
     -v /mydata/nginx/logs:/var/log/nginx \
     -v /mydata/nginx/conf/:/etc/nginx \
     -d nginx:1.10
    
  • 设置开机启动nginx

    docker update nginx --restart=always
    
  • 创建“/mydata/nginx/html/index.html”文件,测试是否能够正常访问

    echo '<h2>hello nginx!</h2>' >/mydata/nginx/html/index.html
    

    访问:http://ngix所在主机的IP:8081/index.html

    image-20220108214636252

2.域名绑定云服务器

2.1域名解析到服务器

image-20220108214746195

2.2访问域名测试

image-20220108214829322

3.让nginx进行反向代理商城服务

image-20220108190710655

配置方法:

image-20220108214949877

3.0在路由器中配置端口映射,将电脑端口映射到公网

image-20220108201456988

3.1查看docker容器中 的nginx配置

[root@VM-4-14-centos nginx]# ls
conf html logs
[root@VM-4-14-centos nginx]# cd conf/
[root@VM-4-14-centos conf]# ls
conf conf.d fastcgi_params koi-utf koi-win mime.types modules nginx.conf scgi_params uwsgi_params win-utf
[root@VM-4-14-centos conf]# vi nginx.conf #查看nginx的配置文件

###############全局块配置
user  nginx;  #用户组
worker_processes  1; #进程

error_log  /var/log/nginx/error.log warn;  #错误日志 位置
pid        /var/run/nginx.pid;  #pid文件位置

##############事件块配置
events {
 worker_connections  1024; #最大连接数
}

#################http请求配置
http {
 include       /etc/nginx/mime.types; #mime类型
 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;

#包含该/etc/nginx/conf.d路径下的所有*.conf配置文件
 include /etc/nginx/conf.d/*.conf;

3.2查看default.conf 默认配置

[root@VM-4-14-centos conf]# cd conf.d/
[root@VM-4-14-centos conf.d]# ls
default.conf
[root@VM-4-14-centos conf.d]# cat default.conf

##############server块
server {
    listen       80; #监听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; #默认首页为index.html
}

#error_page  404              /404.html; #404页面

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

[root@VM-4-14-centos conf.d]#

3.3配置nginx通过域名访问gulimall的商城服务

3.3.1,将默认的配置文件复制一份做gulimall的配置文件

image-20220108192623896

3.3.2修改gulimall.conf文件

[root@VM-4-14-centos conf.d]# docker logs nginx #查看docker中容器nginx的日志
nginx: [emerg] unexpected "}" in /etc/nginx/conf.d/gulimall.conf:12
nginx: [emerg] unexpected "}" in /etc/nginx/conf.d/gulimall.conf:12
[root@VM-4-14-centos conf.d]# vi gulimall.conf #编辑gulimall.conf配置文件

server {
    listen       80;
	#配置网站地址域名 
	#这里放自己绑定本云服务器的域名,就是你的域名
    server_name  gulimall.xxxxxw.club; 

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

    location / {
	#反向代理到我的电脑的gulimall的商城服务
	#这个ip是你的电脑公网ip地址 +端口号
	proxy_pass http://182.xxx.xxx.xxx: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;
    }

    # 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.3.2.1绑定本云服务器的域名

image-20220108215233511

3.3.3重启nginx服务

[root@VM-4-14-centos conf.d]# docker restart nginx #重启nginx服务
nginx
[root@VM-4-14-centos conf.d]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
deef34e0300c nginx:1.10 "nginx -g 'daemon of…" 19 hours ago Up 6 seconds 443/tcp, 0.0.0.0:8081->80/tcp nginx

3.3.4浏览器打开网页访问你的域名+端口号
http://gulimall.xxxxx.club:8081/

出现如下界面说明配置成功

image-20220108215330838

4.配置nginx负载均衡网关

负载均衡配置文档官方http://nginx.org/en/docs/http/load_balancing.html

例子:

http {
    upstream myapp1 { 上游服务器地址
        server srv1.example.com;
        server srv2.example.com;
        server srv3.example.com;
    }

    server {
        listen 80; 

        location / { #请求转发
            proxy_pass http://myapp1; #代理到上游服务器地址
        }
    }
}

例子介绍:

image-20220108202529218
4.1在路由器处设置端口转发,将本机ip的88端口转发到公网ip的88端口

image-20220108215412985

4.2在nginx处配置

4.2.1配置/mydata/nginx/conf路径下的nginx.conf 文件

[root@VM-4-14-centos conf]# ls
conf conf.d fastcgi_params koi-utf koi-win mime.types modules nginx.conf scgi_params uwsgi_params win-utf
[root@VM-4-14-centos conf]# vi nginx.conf
[root@VM-4-14-centos conf]# cat 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;
 #↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
 #↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
 #在这里配置上游服务器,,也就是自己电脑的ip个谷粒商城的网关端口号
 #配置上游服务器
 upstream gulimall{
 #配置本机电脑的谷粒商城的服务网关的地址:端口号
	server 182.xxx.xxx.xx4:88;
 }
 #↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑
	#↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑
 include /etc/nginx/conf.d/*.conf;
}

4.2.2配置/mydata/nginx/conf/conf.d下gulimall.conf配置文件

[root@VM-4-14-centos conf]# cd conf.d/
[root@VM-4-14-centos conf.d]# ls
default.conf gulimall.conf
[root@VM-4-14-centos conf.d]# vi gulimall.conf
[root@VM-4-14-centos conf.d]# cat gulimall.conf

server {
 listen       80;
#配置网站地址域名 这里配置你的域名
 server_name  gulimall.xxxw.club;

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

 location / {
 #↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
 #↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
#反向代理到我的电脑的gulimall的商城服务,这里填你的电脑的公网ip+商城服务端口号
	####proxy_pass http://182.xxx.xxx.xx4:10000;
    ####负载均衡到上游服务器,就是上一步配置的地方的名字
     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;
 }

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

4.2.3重启nginx服务

[root@VM-4-14-centos conf.d]# docker restart nginx #重启nginx服务
nginx
[root@VM-4-14-centos conf.d]# ls
default.conf gulimall.conf
[root@VM-4-14-centos conf.d]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
deef34e0300c nginx:1.10 "nginx -g 'daemon of…" 20 hours ago Up 5 seconds 443/tcp, 0.0.0.0:8081->80/tcp nginx

4.3在gulimall-gateway处配置文件application.yml增加断言-->这个配置一定要放在配置最后

spring gateway路由规则https://docs.spring.io/spring-cloud-gateway/docs/2.2.10.BUILD-SNAPSHOT/reference/html/#the-host-route-predicate-factory

####################################################################
        #将域名访问路由到商品服务       
        - id: gulimall_host  #路由名,nginx负载均衡
          uri: lb://gulimall-product  #负载均衡到商品服务
          predicates: #断言
            - Host=gulimall.xxxw.club #gulimall.xxxw.club下的请求都转给gulimall-product服务
#            例如:访问http://gulimall.xxxw.club:8081/api/product/attrattrgrouprelation/list 路由到http://localhost:10000//product/attrattrgrouprelation/list
#
4.4.别忘了启动nacos注册中心
4.5浏览器进行访问测试:你的域名+端口号+网关处配置的api

http://gulimall.xxxxw.club:8081/api/product/attrattrgrouprelation/list

出现如下界面,说明配置成功

image-20220108215724687

5.nginx代理给网关的时候,不让丢失请求host信息

image-20220108212628790
  • nginx代理给网关的时候,会丢失请求的host信息
    解决办法:proxy_set_header Host $host
5.1编辑gulimall.conf 文件,设置host信息

[root@VM-4-14-centos conf.d]# pwd
/mydata/nginx/conf/conf.d
[root@VM-4-14-centos conf.d]# ls
default.conf gulimall.conf
[root@VM-4-14-centos conf.d]# vi gulimall.conf
[root@VM-4-14-centos conf.d]# cat gulimall.conf

server {
    listen       80;
#配置网站地址域名 不知道ip可不可以...
    server_name  gulimall.walmw.club;

    #charset koi8-r;
    #access_log  /var/log/nginx/log/host.access.log  main;
    location / {
    #↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
    #nginx代理给网关的时候,不让丢失请求host信息
	proxy_set_header Host $host;
	#↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑
#反向代理到我的电脑的gulimall的商城服务
	#proxy_pass http://182.xxx.xxx.xx4:10000;
        #负载均衡到上游服务器
        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;
    }

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

5.2重启nginx服务

[root@VM-4-14-centos conf.d]# docker restart nginx
nginx
[root@VM-4-14-centos conf.d]#

5.3测试访问域名

访问http://gulimall.xxxw.club:8081/

出现如下界面,说明配置成功

image-20220108215820361

6.总结:域名映射效果

请求接口 gulimall.com
请求页面 gulimall.com

nginx直接代理给网关,网关判断
如果/api/****,转交给对应的服务器
如果是 满足域名,转交给对应的服务

0

评论区