nginx 做反向代理

  • 时间:2018-11-13

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; } }
最后附上Nginx的重启命令
进入到Nginx的安装目录下的sbin目录,执行./nginx -t 检查Nginx的配置文件是否正确;执行./nginx -s reload 重启服务
Top