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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM