nginx 做反向代理
Posted LTmei
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了nginx 做反向代理相关的知识,希望对你有一定的参考价值。
1、nginx的常用配置大家可以去搜一下,有很多优秀的博客,我这篇文章要实现的需求是:
a.根据访问的域名不同,跳转到不同的项目(html首页,80端口)
b.拦截访问中所有带有api的请求,转发到后端的不同服务器中(Tomcat项目,任意端口)
2、下面是Nginx的nginx.conf配置文件
user root root; worker_processes 1; #worker_cpu_affinity 1; worker_rlimit_nofile 60000; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; pid /var/run/nginx.pid; events { use epoll; worker_connections 60000; } 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 50m; sendfile on; tcp_nopush on; keepalive_timeout 120; server_tokens off; tcp_nodelay on; #ws # map $http_upgrade $connection_upgrade { # default upgrade; # ‘‘ close; # } 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; #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 "msie6"; #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; access_log off; include vhost/*.conf; }
所有自定义的配置 都在vhost文件夹下面
3、现在贴出两个vhost文件夹下面的自定义配置
server { listen 80; server_name dev.hr.static.baidu.com; location ~ .*.(gif|jpg|jpeg|png|bmp|swf|flv|ico|js|css|html|woff2|woff)$ { root /deploy/www/baidu-www/web-hr; add_header Access-Control-Allow-Origin *; add_header Access-Control-Allow-Headers X-Requested-With; add_header Access-Control-Allow-Methods GET,POST,OPTIONS; } } server { listen 80; server_name dev.am.static.baidu.com; location ~ .*.(gif|jpg|jpeg|png|bmp|swf|flv|ico|js|css|html|woff2|woff)$ { root /deploy/www/baidu-www/web-am; add_header Access-Control-Allow-Origin *; add_header Access-Control-Allow-Headers X-Requested-With; add_header Access-Control-Allow-Methods GET,POST,OPTIONS; } }
这个是监听80端口,注意server_name,这个服务名跟你访问的host要一致才可以实现,如果你要进入到hr的首页,那么你在浏览器的输入是dev.hr.static.baidu.com,这样才能正常访问到你指定的页面
server { listen 80; server_name am.dev.baidu.com; access_log logs/am.baidu.com_nginx.log combined; index index.html index.jsp index.php; location ^~ /api { proxy_pass http://127.0.0.1:8081/;#需要代理的地址 include proxy.conf; } location ^~ / { root /deploy/www/baidu-www/web-am/; } }
这个是需要转发的后端服务,这个路径后面可以带项目名,同样也需要注意server——name
以上是关于nginx 做反向代理的主要内容,如果未能解决你的问题,请参考以下文章