nginx 设置反向代理

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

参考地址:https://www.cnblogs.com/hanmk/p/9289069.html

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

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; } }

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

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

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

    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;
        }
    }

三、nginx导入外部配置文件

参考地址:https://blog.csdn.net/u012946310/article/details/79555968

#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;
}
原文地址:https://www.cnblogs.com/liyuanhong/p/11661867.html