【nginx】配置反向代理nginx 反向代理 配置
以MacOS系统为例,这里只是记录最简单版本,仅用于本地测试。安装推荐使用homebrew。配置文件默认为/usr/local/etc/nginx/nginx.conf。
worker_processes 1;error_log logs/error.log;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
access_log logs/access.log main;
keepalive_timeout 65;
include /Users/miracle/nginx/*.conf;
}
在nginx.conf配置文件中,配置了http模块,其下的server模块配置使用include方式导入。这样可以将不同域名的配置模块化,结构更清晰。下面是域名的配置:
upstream backend {server 127.0.0.1:8080;
}
server {
listen 8081;
server_name ;
location / {
proxy_pass http://backend;
}
}
首先定义了名为backend的upstream,里面只配置了一台http服务器。然后是server模块的配置,监听了8081端口,并且将该域名下的全部请求转发至127.0.0.1:8080下。另外,upstream是全局共享的,如果其他模块配置了同名的upstream,会报错。还有,这个域名默认是无法解析的,需修改本地hosts文件。
运行nginx -s reload命令重启nginx。
我在本地起了一个springmvc的应用,监听的是8080端口。那么直接访问如下url会失败
http://:8081/springmvcxml/hello但是由于在nginx配置了反向代理,所以请求可以被响应。
curl http://:8081/springmvcxml/hellohello