Nginx,不用多说啦,大家都熟悉的不能再熟悉了,它是一款轻量级的高性能Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,最近在本地研究将nginx和resin配合使用,使服务性能达到最高,在配置过程中主要涉及到单域名配置代理服务,以及配置多域名代理服务,以及最简单实现跨域配置(当然什么负载均衡,动静分离,静态资源代理这些就不说啦,直接放到代码里去了,有注释)。

在正式上线前,先在本地window环境下配置跑起来测试下配置是否正确,所以这次就以windows 版的nginx做测试了,正式上线后,配置也就相差无几了。

一、nginx下载、安装及启动 
下载地址:nginx 
下载最新版的nginx for windows版本,下载完成后,解压做zip包到本地磁盘上,例如:D:\hwy\nginx-1.8.0 
启动:

1、D:\hwy\nginx-1.8.0\start nginx(推荐)
2、D:\hwy\nginx-1.8.0\nginx.exe
  • 1
  • 2

注意:推荐使用第一种方式启动,因为第二种方式会使你的cmd窗口一直处于运行状态,没法输入其他命令了,而第一种已后台的方式运行,可以继续输入其他命令。 
停止:

D:\hwy\nginx-1.8.0\nginx -s stop
  • 1

重启:

D:\hwy\nginx-1.8.0\nginx -s reload
  • 1

二、配置单个server代理服务 
为了模拟域名的形式访问本地服务,我们修改windows的host文件,新增

127.0.0.1 a.test.com
127.0.0.1 b.test.com #(待会配置多域名时使用)
  • 1
  • 2

在D:\hwy\nginx-1.8.0\conf目录新增一个nginx-resin-a.conf,基本配置代码如下:

server{
listen 80;
server_name a.test.com;
index index.html index.htm;
root D:/hwy/aTest/src/main/webapp/;
#配置首页精确匹配
location = / {
proxy_next_upstream http_502 http_504 error timeout invalid_header;
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_pass http://aTestServer;
}
#配置首页
location / {
proxy_next_upstream http_502 http_504 error timeout invalid_header;
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_pass http://aTestServer;
}
#动态页面交给http://127.0.0.1:8080,也即我们之前在nginx.conf定义的upstream aTestServer 均衡
location ~ .*\.(php|jsp|cgi)?$ {
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_pass http://aTestServer;
}
#配置静态资源前缀匹配
location ~ ^/(fileUpload|doc)/ {
root D:/hwy/www/aTest/;
access_log off;
expires 4d;
}
#配置Nginx动静分离,定义的静态页面直接从项目指定目录读取。
location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$ {
access_log off;
expires 30d;
} #定义Nginx输出日志的路径
access_log D:/hwy/logs/aTest/access.log main;
error_log D:/hwy/logs/aTest/error.log crit;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44

将nginx-resin-a.conf引入到nginx.conf文件里面,nginx.conf如下:

#user  nobody;
#工作的子进程数量(通常等于CPU数量或者2倍于CPU)
worker_processes 4; #错误日志存放路径
#error_log logs/error.log;
#error_log logs/error.log notice;
error_log logs/error.log info; #指定pid存放文件
pid logs/nginx.pid; worker_rlimit_nofile 51200; events {
#使用网络IO模型linux建议epoll,FreeBSD建议采用kqueue,window下不指定。
#use epoll;
#允许最大连接数
worker_connections 51200;
} http {
include mime.types;
default_type application/octet-stream; #定义日志格式
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"'; #access_log D:/hwy/nginx-1.8.0/logs/access.log main; client_header_timeout 3m;
client_body_timeout 3m;
send_timeout 3m; client_header_buffer_size 1k;
large_client_header_buffers 4 4k; sendfile on;
tcp_nopush on;
tcp_nodelay on; #keepalive_timeout 0;
keepalive_timeout 120; upstream aTestServer {
server 127.0.0.1:8080;
} include nginx-resin-a.conf;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52

好了,经过上边的配置之后,我们启动nginx以及本地你的aTest服务resin,访问下http://a.test.com看看效果吧!

三、配置多域名 
上边配置了一个aTest的服务的代理,如果我们在服务器上边要运行多个服务,比如bTest服务,达到的效果是,通过http://a.test.com访问aTest站点服务,通过http://b.test.com访问bTest站点服务,其实也很简单的,只需要在引入一个bTest的server配置即可。 
在D:\hwy\nginx-1.8.0\conf目录新增一个nginx-resin-b.conf,基本配置代码如下:

server{
listen 80;
server_name b.test.com;
index index.html index.htm;
root D:/hwy/bTest/src/main/webapp/;
#配置首页精确匹配
location = / {
proxy_next_upstream http_502 http_504 error timeout invalid_header;
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_pass http://bTestServer;
}
#配置首页
location / {
proxy_next_upstream http_502 http_504 error timeout invalid_header;
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_pass http://bTestServer;
}
#动态页面交给http://127.0.0.1:8090,也即我们之前在nginx.conf定义的upstream bTestServer 均衡
location ~ .*\.(php|jsp|cgi)?$ {
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_pass http://bTestServer;
}
#配置静态资源前缀匹配
location ~ ^/(fileUpload|doc)/ {
root D:/hwy/www/bTest/;
access_log off;
expires 4d;
}
#配置Nginx动静分离,定义的静态页面直接从项目指定目录读取。
location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$ {
access_log off;
expires 30d;
} #定义Nginx输出日志的路径
access_log D:/hwy/logs/bTest/access.log main;
error_log D:/hwy/logs/bTest/error.log crit;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44

然后我们只需要将nginx-resin-b.conf引入到nginx.conf配置文件中即可,在nginx.conf的http最后边增加

    upstream bTestServer {
server 127.0.0.1:8090;
} include nginx-resin-b.conf;
  • 1
  • 2
  • 3
  • 4
  • 5

好了,现在重启nginx,并启动本地你的bTest服务resin,分别访问下http://a.test.comhttp://b.test.com看看是不是都到达指定的站点服务上去啦!

四、跨域配置 
好了,现在我们有了两个不同域名指定的项目了,但是现在bTest服务中有些接口数据请求需要由aTest来提供,bTest通过ajax请求aTest的接口数据,这个时候,如果直接请求,肯定是会涉及到跨域的问题了,浏览器是不允许的。现在我们可以通过nginx反向代理来实现跨域请求。

实例一: 
在nginx-resin-b.conf配置中增加如下:

    location /api {
rewrite ^.+api/?(.*)$ /api/$1 break;
proxy_pass http://aTestServer;
}
  • 1
  • 2
  • 3
  • 4

注意:这里意思是将所有http://b.test.com/api/xxx类似的请求直接rewrite到http://a.test.com/api/xxx上边去啦!

实例二: 
在nginx-resin-b.conf配置中增加如下:

    location /baidu {
rewrite ^.+baidu/?(.*)$ /$1 break;
proxy_pass http://www.baidu.com;
}
  • 1
  • 2
  • 3
  • 4

注意:这里我们就把baidu网站整个搬到我们的127.0.0.1:8080/baidu/目录下了,这样我们访问的时候,直接通过/baidu/xxx来请求百度的数据啦!

简而言之,nginx 是通过把本地一个url前缀映射到要跨域访问的web服务器上,就可以实现跨域访问。

对于浏览器来说,访问的就是同源服务器上的一个url。而nginx通过检测url前缀,把http请求转发到后面真实的物理服务器,并通过rewrite命令重新指向真实的请求地址。这样真实的服务器就可以正确处理请求,并且并不知道这个请求是来自代理服务器的。

简单说,nginx服务器欺骗了浏览器,让它认为这是同源调用,从而解决了浏览器的跨域问题。又通过重写url,欺骗了真实的服务器,让它以为这个http请求是直接来自与用户浏览器的。

windows上 nginx 配置代理服务,配置多域名,以及最简单实现跨域配置的更多相关文章

  1. 014.Nginx跨域配置

    一 跨域概述 1.1 同源策略 同源策略是一个安全策略.同源,指的是协议,域名,端口相同.浏览器处于安全方面的考虑,只允许本域名下的接口交互,不同源的客户端脚本,在没有明确授权的情况下,不能读写对方的 ...

  2. 跨域原因及SpringBoot、Nginx跨域配置

    目录 概述 简单请求 跨域解决方案 概述 SpringBoot跨域配置 Nginx跨域配置 概述 MDN文档 Cross-Origin Resource Sharing (CORS) 跨域的英文是Cr ...

  3. nginx-springboot-vue前后端分离跨域配置

    nginx-springboot-vue前后端分离跨域配置 引言 接着上篇--简单的springboot-vue前后端分离登录Session拦截的demo,其中跨域是通过springboot后端全局设 ...

  4. 常见跨域解决方案以及Ocelot 跨域配置

    常见跨域解决方案以及Ocelot 跨域配置 Intro 我们在使用前后端分离的模式进行开发的时候,如果前端项目和api项目不是一个域名下往往会有跨域问题.今天来介绍一下我们在Ocelot网关配置的跨域 ...

  5. Django+Vue跨域配置与经验

    一.原理 同源?同源策略? 同源的定义是:两个页面的协议.端口和域名都相同 同源的例子: 不同源的例子: 同源策略SOP(Same origin policy)是一种浏览器约定,它是浏览器最核心也最基 ...

  6. Asp.net跨域配置

    <system.webServer> <httpProtocol> <customHeaders> <add name="Access-Contro ...

  7. asp.net (webapi) core 2.1 跨域配置

    原文:asp.net (webapi) core 2.1 跨域配置 官方文档 ➡️ https://docs.microsoft.com/zh-cn/aspnet/core/security/cors ...

  8. .net core api服务端跨域配置

    第1步:添加包引用(.net core 2.2 已自带此包,可跳过此步骤) Install-Package Microsoft.AspNetCore.Cors 第2步:在Startup.cs文件的Co ...

  9. asp.net core api 跨域配置

    项目前后端分离,前端请求接口例如使用axios发送请求时浏览器会提示跨域错误,需要后端配置允许接口跨域 配置步骤: 1.通过NuGet安装Microsoft.AspNetCore.Cors.dll类库 ...

随机推荐

  1. jvm运行时数据区之程序计数器

    什么是程序计数器? 程序计数器是一块 较小 的内存空间,它可以看做是当前线程所执行的字节码的 行号指示器 :在虚拟机的概念模型里(仅仅是概念模型,各种虚拟机可能会通过一些更高效的方式去实现),字节码解 ...

  2. linux部署django项目流程(全)

    1.python3和python2共存配置 流程在下面网址中 https://www.cnblogs.com/vinic-xxm/p/11358894.html 2.安装依赖包 yum install ...

  3. SpringBoot配置多注册中心(yml,properties)

    dubbo-2.6.6 dubbo.config.multiple=true    dubbo.registries.z1.timeout = 5000    dubbo.registries.z1. ...

  4. 大数据技术原理与应用【第五讲】NoSQL数据库:5.5 从NoSQL到NewSQL数据库

    应用场景: OldSql数据库:希望一种架构就能支持多种应用场景,但证明不可能.   NewSql数据库:同时具备OldSql和NoSQL各自的优点:水平可扩展性,强一致性,事务一致性,支持查询,支持 ...

  5. PHP中md5()函数绕过

    PHP md5()函数的简单绕过方法,该篇作为学习笔记简单记录一下.   例题   例题链接: http://ctf5.shiyanbar.com/web/houtai/ffifdyop.php   ...

  6. 201871010131-张兴盼《面向对象程序设计(java)》第二周学习总结

    项目 内容 <面向对象程序设计(java)> https://home.cnblogs.com/u/nwnu-daizh/ 这个作业的要求在哪里 https://www.cnblogs.c ...

  7. 20180610模拟赛T4——木棍

    有\(N\)根木棍,每根的长度\(L\)和重量\(W\)已知.这些木棍将被一台机器一根一根地加工.机器需要一些启动时间来做准备工作,启动时间与木棍被加工的具体情况有关.启动时间遵循以下规则: 加工第一 ...

  8. js检测手机类型(android,ios,blackberry,windows等)

    var isMobile = { Android: function() { return navigator.userAgent.match(/Android/i); }, BlackBerry: ...

  9. Hbase优化:(待重点研究)

    一.服务端调优 1.参数配置 1).hbase.regionserver.handler.count:该设置决定了处理RPC的线程数量,默认值是10,通常可以调大,比如:150,当请求内容很大(上MB ...

  10. memcpy和strcpy的区别

    strcpy和memcpy主要有以下3方面的区别. 复制的内容不同.strcpy只能复制字符串,而memcpy可以复制任意内容,例如字符数组.整型.结构体.类等. 复制的方法不同.strcpy不需要指 ...