翼度科技»论坛 云主机 服务器技术 查看内容

Nginx 搭建域名访问环境的详细过程

5

主题

5

帖子

15

积分

新手上路

Rank: 1

积分
15
1.Nginx配置文件


  1. server {
  2.     listen       80;
  3.     server_name  www.gulimall.com;
  4.     #charset koi8-r;
  5.     #access_log  /var/log/nginx/log/host.access.log  main;
  6.     location / {
  7.         proxy_pass http://192.168.232.1:10001;
  8.     }
  9.     #error_page  404              /404.html;
  10.     # redirect server error pages to the static page /50x.html
  11.     #
  12.     error_page   500 502 503 504  /50x.html;
  13.     location = /50x.html {
  14.         root   /usr/share/nginx/html;
  15.     }
  16.     # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  17.     #
  18.     #location ~ \.php$ {
  19.     #    proxy_pass   http://127.0.0.1;
  20.     #}
  21.     # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  22.     #
  23.     #location ~ \.php$ {
  24.     #    root           html;
  25.     #    fastcgi_pass   127.0.0.1:9000;
  26.     #    fastcgi_index  index.php;
  27.     #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
  28.     #    include        fastcgi_params;
  29.     #}
  30.     # deny access to .htaccess files, if Apache's document root
  31.     # concurs with nginx's one
  32.     #
  33.     #location ~ /\.ht {
  34.     #    deny  all;
  35.     #}
  36. }
复制代码
让所有的请求都转到,商品服务


2.Nginx 搭建转发网关进行负载均衡


nginx.conf
  1. user  nginx;
  2. worker_processes  1;
  3. error_log  /var/log/nginx/error.log warn;
  4. pid        /var/run/nginx.pid;
  5. events {
  6.     worker_connections  1024;
  7. }
  8. http {
  9.     include       /etc/nginx/mime.types;
  10.     default_type  application/octet-stream;
  11.     log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
  12.                       '$status $body_bytes_sent "$http_referer" '
  13.                       '"$http_user_agent" "$http_x_forwarded_for"';
  14.     access_log  /var/log/nginx/access.log  main;
  15.     sendfile        on;
  16.     #tcp_nopush     on;
  17.     keepalive_timeout  65;
  18.     #gzip  on;
  19.         upstream gulimall{
  20.                 server 192.168.232.1:88;
  21.         }
  22.     include /etc/nginx/conf.d/*.conf;
  23. }
复制代码
gulimall.conf
  1. server {
  2.     listen       80;
  3.     server_name  www.gulimall.com;
  4.     location / {
  5.         proxy_pass http://gulimall;
  6.     }
  7. }
复制代码
网关 application.yml
  1.         - id: gulimall_host_route
  2.           uri: lb://gulimall-product
  3.           predicates:
  4.             - Host=www.gulimall.com
复制代码
有个坑

因为nginx代理给网关的时候会 丢掉请求头里的host ,所以网关的匹配规则没有匹配上导致404

需要配置Nginx
在gulimall.conf
  1. location / {
  2.         proxy_set_header Host $host;
  3.                 proxy_pass http://gulimall;
  4.     }
复制代码



3.实现动静分离
  1. location /static {
  2.         root   /usr/share/nginx/html;
  3.                 autoindex on;  #开启文件目录结构在网页显示
  4.     }
复制代码
注意路径会拼成,  /usr/share/nginx/html/static


4.还有一个坑


这个匹配规则不要放前面,因为这些请求都是从上往下匹配的,匹配到了,就不往下匹配了,然而往nginx发送的域名都是 www.gulimall.com 所以,都会走第一个匹配,下面这些就匹配不上,就找不到那个接口就404了。
做好两点:
  1.请求接口  http://域名/api/xxx
   2.请求页面 http://域名
转发给网关,通过网关路由到对应的服务 !
到此这篇关于Nginx 搭建域名访问环境的文章就介绍到这了,更多相关Nginx 域名访问环境内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

来源:https://www.jb51.net/server/323193t1g.htm
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x

举报 回复 使用道具