nginx简单配置
反向代理
反向代理图示
配置文件nginx.conf
只需配置下 server中的 proxy_pass 就行,其他的都是默认的。
#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;
#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;
# upstream myserver {
# server 222.182.202.16:8056;
# server 222.182.202.16:8056;
# }
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://127.0.0.1:8080/log-test/;
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;
# }
#}
}
配置解释
server_name 可配置为ip地址(默认为localhost),也可配置为域名:
同一个listen只能配置一个ip,但是域名可配置为多个(一个ip可由多个域名指向)
server {
listen 80;
server_name 47.97.156.184;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://127.0.0.1:8080/log-test/;
root html;
index index.html index.htm;
}
....
server {
listen 80;
server_name xl.domain.com; #指向 47.97.156.184
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://127.0.0.1:8080/log-test/;
root html;
index index.html index.htm;
}
....
server {
listen 80;
server_name yy.domain.com; #同样指向 47.97.156.184
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://127.0.0.1:8080/log-test/;
root html;
index index.html index.htm;
}
....
其中, location下的 proxy_pass 配置规则:只要是可以访问的地址均可。
例子
因为只有一台云服务器,所以将nginx和tomcat都安装在这一台服务器上,并且没有域名,所以直接使用ip地址进行配置,只是不能演示使用不同域名配置多个server {
listen 80;
server_name
…
}
所以就只配置一个server.
在服务器上安装tomcat
- 端口监听8080
- 部署测试的项目log-test,访问测试
nginx.conf配置
启动tomcat、nginx
浏览器输入 nginx.conf配置的server_name + listen, 因为是listen是80,所以可以省略
结论
nginx通过配置server_name + listen 成功代理了服务器项目proxy_pass
负载均衡
将负载(请求)平均(可通过策略配置调整)分摊到多个服务器上去
配置
- 在http块内 server块外配置要分摊的服务器列表,形式: ip(域名):端口
- 在server块中配置服务器列表的名称 myserver
准备
准备8080 和 8081,直接访问测试(不走nginx),看下效果
通过nginx测试负载均衡
访问配置的server_name和listen :
- 第一次访问
- 再次刷新访问
- 配置的nginx发挥了如上作用
动静分离
这里只演示配置静态资源
可以是nginx本地服务器上资源
配置 server—>location
在服务上的有相应的目录
浏览器访问静态资源文件 “how tomcat works.pdf”
访问成功,这个与tomcat配置虚拟路径是一样的,参见:tomcat配置虚拟路径
访问其他服务器上的静态资源
浏览器访问:
总结
nginx的三大功能:反向代理、负载均衡、动静分离,上面展示的只是最基础的一部分,还有很多配置和功能,待后续了解。