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

下面是反向proxy.conf的配置

表示使nginx阻止HTTP应答代码为400或者更高的应答
#proxy_intercept_errors on;
proxy_redirect off;
#以下三行,目的是将代理服务器收到的用户的信息传到真实服务器上
#后端的Web服务器可以通过X-Forwarded-For获取用户真实IP
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Ssl on;
proxy_set_header Accept-Encoding '';
proxy_set_header Referer $http_referer;
proxy_set_header Cookie $http_cookie; #允许客户端请求的最大单文件字节数
client_max_body_size 10m;
#缓冲区代理缓冲用户端请求的最大字节数
client_body_buffer_size 128k;
#nginx跟后端服务器连接超时时间(代理连接超时)
proxy_connect_timeout 300;
#连接成功后,后端服务器响应时间(代理接收超时)
proxy_read_timeout 900;
#设置代理服务器(nginx)保存用户头信息的缓冲区大小
proxy_buffer_size 32k;
#proxy_buffers缓冲区,网页平均在32k以下的话,这样设置
proxy_buffers 4 64k;
#高负荷下缓冲大小(proxy_buffers*2)
proxy_busy_buffers_size 128k;
#设定缓存文件夹大小,大于这个值,将从upstream服务器传
proxy_temp_file_write_size 64k; proxy_send_timeout 900; proxy_hide_header Vary;

最后附上Nginx的重启命令

进入到Nginx的安装目录下的sbin目录,执行./nginx -t 检查Nginx的配置文件是否正确;执行./nginx -s reload 重启服务

最新文章

  1. why add \n to http response.responseText
  2. vector,arraylist, linkedlist的区别是什么
  3. 【BZOJ】1269: [AHOI2006]文本编辑器editor(Splay)
  4. ECSHOP订单一键发货简化订单发货流程
  5. oracle redo日志维护
  6. maven 依赖冲突的问题
  7. java学习面向对象之设计模式之单例模式
  8. Java学习日记9-异常
  9. Ueditor使用方法
  10. Struts2配置拦截器自定义栈时抛异常:Unable to load configuration. - interceptor-ref - file:/D:/tomcat_install/webapps/crm/WEB-INF/classes/struts.xml
  11. ElasticSearch核心知识 -- 索引过程
  12. CentOS使用@Value注解为属性赋值的时候出现乱码
  13. C-sizeof和strlen区别,以及sizeof如何计算结构体大小
  14. js 学习之路8:for循环
  15. 069、Calico的默认连通性(2019-04-12 周五)
  16. eclipse 打开时一闪而过解决办法
  17. 将Long类型字节大小数据转换成标准的视频大小格式
  18. jquery单击事件的写法
  19. cocos 搭建安卓环境
  20. Vue的计算属性和侦听器

热门文章

  1. HTML5:使用Canvas和Input range控件放大缩小图片,剪裁,并上传图片
  2. UVA11722 Jonining with Friend
  3. SELinux安全方式
  4. Redis源码解析:11RDB持久化
  5. PYTHON__ ITERTOOLS模块
  6. Spring.Net2.0+NHibernate4.0 +Asp.Net Mvc4 二
  7. html5绘图工具选择
  8. div style标签内嵌CSS样式
  9. 木卯先生的笔记---Date类、DateFormat类和Calendar类
  10. 老大让我看baidu他们的查公交是怎么做的,我就看了