1. 程式人生 > >nginx下配置多個web服務

nginx下配置多個web服務

一、nginx簡介:

Nginx("engine x")是一款是由俄羅斯的程式設計師Igor Sysoev所開發高效能的 Web和 反向代理 伺服器,也是一個 IMAP/POP3/SMTP 代理伺服器
高連線併發的情況下,Nginx是Apache伺服器不錯的替代品。

二、niginx預設conf配置檔案:

#user  nobody; #nginx使用使用者
worker_processes  1;#設定值和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; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on;
  #下面是server虛擬主機的配置 server { listen
80; #監聽埠 server_name localhost; #域名 #charset koi8
-r; #access_log logs/host.access.log main; location / { root html; #站點目錄 index index.html index.htm; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} } # another virtual host using mix of IP-, name-, and port-based configuration # #server { # listen 8000; # listen somename:8080; # server_name somename alias another.alias; # location / { # root html; # index index.html index.htm; # } #} # HTTPS server # #server { # listen 443 ssl; # server_name localhost; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } #} }

三、nginx下配置多個web工程:

詳情如下:(aa是請求介面)

1.web工程1:

  • tomcat的訪問路徑:http://localhost:18880/aa
  • nginx的訪問路徑:http://localhost:8888/first-path/aa

2.web工程1:

  • tomcat的訪問路徑:http://localhost:18881/first-path/aa
  • nginx的訪問路徑:http://localhost:8888/firstPath/aa
#關鍵配置如下
http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    keepalive_timeout  65;

    #web工程1
    upstream nana {
        server localhost:18880;
    }

    #web工程2
    upstream nananana {
        server localhost:18881;
    }
        
    server {
        listen       8888;
        server_name  localhost;

        location / {
            root   html;
            index  index.html index.htm;
        } 

        #web工程1    
        location ^~/first-path/ { 
            proxy_pass http://nana/;
            proxy_set_header X-real-ip $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_connect_timeout 5s;
        }

        #web工程2
        location /firstPath { 
            proxy_pass http://nananana/first-path;
            proxy_set_header X-real-ip $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_connect_timeout 5s;
        }
}