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

#访问登录页面有/service1会被代理到访问/usr/local/nginx/html/admin下面的index.html页面
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;
    }
	
#接口访问前缀带有/HYWater的所有请求都会被代理到http://ip:8080,解决前端访问后端跨域错误
	location /HYWater {#代理访问后端接口
        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/目录下

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值