nginx 设置反向代理

1、多个路径指向同一ip的不一样服务

参考地址:http://www.javashuo.com/article/p-rbbrhiir-p.htmljavascript

编辑nginx.conf配置文件,新增长一个server模块,或者在原有的Server模块下增长以下:css

server {
        listen       80;  #监听80端口
        server_name  localhost;

     #注意:把原来的根路径的location注释掉了,这个页面是跳转到nginx的首页,由于不容许出现2个同样的location路径,不然会报错 #location
/ { # root html; # index index.html index.htm; #} #监听80端口,将全部80端口的访问代理到http://127.0.0.1:5000 地址 location = / { proxy_pass http://127.0.0.1:5000; } #监听80端口,将全部http://host/test路径的访问代理到http://127.0.0.1:5000 地址 location = /test { proxy_pass http://127.0.0.1:5001; } } server { listen 8080; #监听8080端口 server_name localhost; #监听80端口,将全部8080端口的访问代理到http://127.0.0.1:8001地址 location = / { proxy_pass http://127.0.0.1:8001; } #监听80端口,将全部http://host/test路径的访问代理到http://127.0.0.1:8002地址 location = /test { proxy_pass http://127.0.0.1:8002; } }

2、多个域名指向同一个ip的不一样服务

参考地址:https://www.linuxidc.com/Linux/2018-10/154702.htmhtml

编辑nginx.conf配置文件,增长一个server模块java

    server {
        listen       80;   #监听80端口
        server_name  www.test.com;   #监听访问的host

        location / {
            #将www.test.com 的访问代理到http://127.0.0.1:5000 地址
            proxy_pass http://127.0.0.1:5000;
        }
    }

    server {
        listen       8080;   #监听8080端口
        server_name  www.test1.com;

        location / {
            #将www.test1.com 的访问代理到http://127.0.0.1:8000 地址
            proxy_pass http://127.0.0.1:8000;
        }
    }

3、nginx导入外部配置文件

参考地址:http://www.javashuo.com/article/p-ulqsekra-gt.htmllinux

#user  nobody;    
worker_processes 1; #nginx工做进程数,通常设置为cpu核数

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

events {
    worker_connections  1024;
}

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  logs/access.log  main;

    sendfile        on;

    #keepalive_timeout  0;
    keepalive_timeout  60;

    client_max_body_size 120M;
    
    gzip on;
    gzip_min_length  1k;
    gzip_buffers     4 16k;
    gzip_http_version 1.0;
    gzip_comp_level 2;
    gzip_types   application/json text/plain application/javascript application/x-javascript text/css application/xml;
    gzip_vary on;
    #gzip  on;

    #导入外部服务器配置文件存放地址
    include /usr/local/nginx/conf/vhosts/*.conf;
}
相关文章
相关标签/搜索