概述
什么是 Nginx?
Nginx (engine x) 是一款轻量级的 Web 服务器 、反向代理服务器及电子邮件(IMAP/POP3)代理服务器。
什么是反向代理?
反向代理(Reverse Proxy)方式是指以代理服务器来接受 internet 上的连接请求,然后将请求转发给内部网络上的服务器,并将从服务器上得到的结果返回给 internet 上请求连接的客户端,此时代理服务器对外就表现为一个反向代理服务器。
安装与使用
安装
详细安装方法请参考:Nginx 安装
使用
nginx 的使用比较简单,就是几条命令。
常用到的命令如下:
nginx -s stop 快速关闭Nginx,可能不保存相关信息,并迅速终止web服务。 nginx -s quit 平稳关闭Nginx,保存相关信息,有安排的结束web服务。 nginx -s reload 因改变了Nginx相关配置,需要重新加载配置而重载。 nginx -s reopen 重新打开日志文件。 nginx -c filename 为 Nginx 指定一个配置文件,来代替缺省的。 nginx -t 不运行,而仅仅测试配置文件。nginx 将检查配置文件的语法的正确性,并尝试打开配置文件中所引用到的文件。 nginx -v 显示 nginx 的版本。 nginx -V 显示 nginx 的版本,编译器版本和配置参数。
80端口配置多个 webapp
#user nobody; worker_processes 1; #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; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; upstream t2.nginx.com { server 127.0.0.1:8082; } upstream t3.nginx.com { server 127.0.0.1:8083; } upstream t1.nginx.com { server 127.0.0.1:8081; } server { listen 80; server_name t1.nginx.com; location / { proxy_pass http://t1.nginx.com; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } server { listen 80; server_name t2.nginx.com; location / { proxy_pass http://t2.nginx.com; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } server { listen 80; server_name t3.nginx.com; location / { proxy_pass http://t3.nginx.com; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }