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