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;
}
相關文章
相關標籤/搜索