栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

nginx 代理前端服务

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

nginx 代理前端服务

主配置文件nginx.conf如下:

user www www;
worker_processes auto;

error_log /data/wwwlogs/error_nginx.log crit;
pid /var/run/nginx.pid;
worker_rlimit_nofile 51200;

events {
  use epoll;
  worker_connections 51200;
  multi_accept on;
}

http {
  include mime.types;
  default_type application/octet-stream;
  server_names_hash_bucket_size 128;
  client_header_buffer_size 32k;
  large_client_header_buffers 4 32k;
  client_max_body_size 1024m;
  client_body_buffer_size 10m;
  sendfile on;
  tcp_nopush on;
  keepalive_timeout 120;
  server_tokens off;
  tcp_nodelay on;

  fastcgi_connect_timeout 300;
  fastcgi_send_timeout 300;
  fastcgi_read_timeout 300;
  fastcgi_buffer_size 64k;
  fastcgi_buffers 4 64k;
  fastcgi_busy_buffers_size 128k;
  fastcgi_temp_file_write_size 128k;
  fastcgi_intercept_errors on;

  #Gzip Compression
  gzip on;
  gzip_buffers 16 8k;
  gzip_comp_level 6;
  gzip_http_version 1.1;
  gzip_min_length 256;
  gzip_proxied any;
  gzip_vary on;
  gzip_types
    text/xml application/xml application/atom+xml application/rss+xml application/xhtml+xml image/svg+xml
    text/javascript application/javascript application/x-javascript
    text/x-json application/json application/x-web-app-manifest+json
    text/css text/plain text/x-component
    font/opentype application/x-font-ttf application/vnd.ms-fontobject
    image/x-icon;
  gzip_disable "MSIE [1-6].(?!.*SV1)";

  #If you have a lot of static files to serve through Nginx then caching of the files' metadata (not the actual files' contents) can save some latency.
  open_file_cache max=1000 inactive=20s;
  open_file_cache_valid 30s;
  open_file_cache_min_uses 2;
  open_file_cache_errors on;

######################## default ############################
  server {
    listen 8081;#监听端口
    server_name localhost;#代理服务器地址,可以是服务器ip,也可以是域名
    access_log /data/wwwlogs/access_nginx.log combined;
    root /data/wwwroot/default;
    index index.html index.htm index.jsp;
    #error_page 404 /404.html;
    #error_page 502 /502.html;
    location /nginx_status {
      stub_status on;
      access_log off;
      allow 127.0.0.1;
      deny all;
    }

location /service1 {#代理前端服务1
        alias   /usr/local/nginx/html/admin;#nginx的html目录下放前端代码
        index  index.html index.htm;
        try_files $uri $uri/ /index.html;
        }
location /service2 {#代理前端服务2
      alias  /usr/local/nginx/html/console;#nginx的html目录下放前端代码,也可以放在其他目录#下
      index index.html  index.htm;
      try_files $uri $uri/ /index.html;
    }
	
	location /admin/ {#代理访问后端接口
        proxy_pass http://ip:8080/;
        proxy_set_header Host $host:$server_port;
    }






  include /usr/local/nginx/conf/picture.conf;#把其他conf配置文件包含进来,使得它们能够生效
}

访问地址:http://47.98.232.105:13001/service1/#/login会代理到/usr/local/nginx/html/admin下的index.html

 访问地址:http://47.98.232.105:13001/service2/#/login会代理到/usr/local/nginx/html/console下的index.html

 

访问后端接口:http://ip:8081/admin/api/v1/login会代理到http://ip:8080/api/v1/login,这一步会把匹配到的/admin/,以及前面的ip和端口代理到http://ip:8080/

 

如果还需要其他配置,比如,访问静态图片,再新建一个辅picture.conf文件,配置如下:

  server {
    listen 8082;
    server_name localhost;
    access_log /data/wwwlogs/access_nginx.log combined;
    root /data/wwwroot/default;
    index index.html index.htm index.jsp;
    #error_page 404 /404.html;
    #error_page 502 /502.html;
    location /nginx_status {
      stub_status on;
      access_log off;
      allow 127.0.0.1;
      deny all;
    }



    location ~ .*.(gif|jpg|jpeg|png|bmp|swf|flv|mp4|ico)$ {
      root /home/project/pic/;#指定图片存放路径
      expires 30d;
      access_log off;
    }
}

该配置文件需要包含在主配置文件中,配置如下:

访问地址:http://ip:8082/031804234d2b44109caf96e09fd991fb.jpg就可以访问到图片了

 

031804234d2b44109caf96e09fd991fb.jpg在 /home/project/pic/目录下

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/361668.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号