1、域名转发
upstream tomcat_taobao {
server 192.168.71.129:8081; #访问的tomcat的IP
}
server {
listen 80; #监听的对外端口
server_name www.taobao.com; #域名
location / {
proxy_pass http://tomcat_taobao; #必须与服务器上对应的tomcat的upstream tomcat_taobao相对应
index index.html index.htm;
}
}
upstream tomcat_sina {
server 192.168.71.129:8082; #访问的tomcat的IP
}
server {
listen 80;#监听的对外端口
server_name www.sina.com;#域名
location / {
proxy_pass http://tomcat_sina; #必须与服务器上对应的tomcat的upstream tomcat_sina相对应
index index.html index.htm;
}
}
2、端口转发
server {
listen 80;
autoindex on;
server_name www.port.com;
access_log c:/access.log combined;
index index.html index.htm index.jsp index.php;
#error_page 404 /404.html;
if ( $query_string ~* ".*[\;'\<\>].*" ) {
return 404;
}
location / {
proxy_pass http://127.0.0.1:8080;
add_header Access-COntrol-Allow-Origin *;
}
}
3、目录转发
server {
listen 80;
autoindex on;
server_name www.stzg.com;
access_log c:/access.log combined;
index index.html index.htm index.jsp index.php;
#error_page 404 /404.html;
if ( $query_string ~* ".*[\;'\<\>].*" ) {
return 404;
}
location / {
root C:\file\img; # \是Windows /是Linux
add_header Access-Control-Allow-Origin *;
}
}
4、负载均衡
//简单的负载均衡,nginx支持ip_hash等分流,也支持插件自定义规则分流
upstream model{
server 127.0.0.1:8080
server 127.0.0.1:8081
server 127.0.0.1:8082
}
server {
listen 80;
server_name localhost;
location / {
proxy_pass model;
proxy_redirect default;
}
}
参考文章
https://blog.csdn.net/weixin_44550490/article/details/93369580
https://blog.csdn.net/kuaizisong/article/details/82790745
https://www.cnblogs.com/meetzy/p/8565507.html