nginx

nginx for Windows

unzip nginx-1.21.4.zip
cd nginx-1.21.4
start nginx

 

nginx -s stop    fast shutdown
nginx -s quit    graceful shutdown
nginx -s reload    changing configuration, starting new worker processes with a new configuration, graceful shutdown of old worker processes
nginx -s reopen    re-opening log files

 

可以在conf目录中新增目录和配置文件,比如 item/item.conf

然后在conf/nginx.conf中添加include

include       item/item.conf

 

Nginx服务Rewrite和Proxy_Pass

 

rewrite

语法: rewrite regex replacement [flag]

regex,用于匹配URI的正则表达式,使用括号()标记要截取的内容

replacement,匹配成功后用户替换URI中被截取内容的字符串,默认情况下,如果该字符串是有"http://"或者"https://"开头的,则不会继续向下对URI进行其他处理,而直接将重写后的URI返回给客户端 

flag,用来设置rewrite对URI的处理行为,可以是下面的标志:

  • last 终止继续在本location块中处理接收到的URI
  • break 将此处重写的URI作为一个新的URI,在本块中继续处理
  • redirect 将重写后的URI返回给客户端,状态码为302
  • permanent 将重写后的URI返回给客户端,状态码为301

 

Nginx 修改访问路径

安装Nginx

http://nginx.org/en/linux_packages.html#RHEL-CentOS

 

配置Nginx

nginx.conf 文件可能在以下目录:/usr/local/nginx/conf/etc/nginx, or /usr/local/etc/nginx

 

 配置域名代理,可以在conf.d目录中添加

 

 分别配置

# saas.conf
server {
    listen 80;
    server_name  saas-export-manager.xxx.net;
    location / {
       proxy_pass http://127.0.0.1:8080;
    }
}
# saas-manager.conf
server {
    listen 80;
    server_name  saas-export.xxx.net;
    location / {
       proxy_pass http://127.0.0.1:8081;
    }
}
View Code

 

 

配置反向代理

server{
    listen    8001;
    server_name    10.202.203.29;
    location /
    {
        proxy_pass    http://10.200.80.21;
    }
}
server{
    listen    8002;
    server_name    localhost;
    location /
    {proxy_pass    http://10.200.151.28;}
}
View Code

访问http://10.202.203.29:8001  打开10.200.80.21

访问http://10.202.203.29:8002  打开10.200.151.28

还可以配置有端口的

server {
    listen 80;
    location / {
        proxy_pass http://localhost:5000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection keep-alive;
        proxy_set_header Host $http_host;
        proxy_cache_bypass $http_upgrade;
    }
}
View Code

这样的话80端口的访问都会转发到5000端口

负载均衡

server{
    listen    8001;
    server_name    10.202.203.29;
    location /
    {
        proxy_pass    http://backend;
    }
}
upstream backend
{
    server    10.200.151.28;
    server    10.200.80.21;
}
View Code

 

 

根据不同的路径转发到不同的端口 这里需要注意关闭selinux

1、临时关闭selinux


setenforce 0            ##设置SELinux 成为permissive模式
setenforce 1    ##设置SELinux 成为enforcing模式


2、永久关闭selinux,


修改/etc/selinux/config 文件
将SELINUX=enforcing改为SELINUX=disabled
重启机器即可

 

server {
    listen       8090;
    server_name  192.168.85.143;
    location /app1/ {
        proxy_pass  http://127.0.0.1:8001/;
        proxy_set_header Host   $host;
        proxy_set_header X-Real-IP      $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
    location /app2/ {
        proxy_pass   http://127.0.0.1:8002/;
        proxy_set_header Host   $host;
        proxy_set_header X-Real-IP      $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
location / {
# 这个配置放到最后
} }

 

posted @ 2018-01-25 10:32  uptothesky  阅读(143)  评论(0编辑  收藏  举报