标签:
本文主要是Nginx做一个简单的反向服务器代理和静态文件缓存。
反向代理(Reverse Proxy)方式是指以代理服务器来接受internet上的连接请求,然后将请求转发给内部网络上的服务器,并将从服务器上得到的结果返回给internet上请求连接的客户端,此时代理服务器对外就表现为一个服务器
我们就开始动手吧。
1. Vistudio 创建两个简单的 WebApplication (Web Forms),一个叫WebApplication1,一个叫 WebApplication2. 为了区别两个应用程序,把default页面做一些修改,用来识别。
比如添加 Application One, Application Two等。
2. 发布两个工程,然后IIS添加两个站点,端口分别用 8050,8060.
3. 测试站点, 输入 http://localhost:8050,http://localhost:8060,均能正常访问
4. 下载安装 Nginx,参考上一篇文章,我这里监听的是 2016端口
5. 找到 c:\nginx-1.9.9\conf\nginx.conf 文件,修改如下
#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 windowsapp.com{
server 127.0.0.1:8050 weight=1;
server 127.0.0.1:8060 weight=1;
}
server {
listen 2016;
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;
#}
location / {
proxy_pass http://windowsapp.com;
}
location ~ \.(css)$ {
root /nginx-1.9.9/staticfiles/;
expires 7d;
}
location ~ \.(js)$ {
root /nginx-1.9.9/staticfiles/;
expires 7d;
}
# 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;
# }
#}
}
关注点:
1. http 下 server listen 配置为 2016,表示监听 2016端口
2. upstream集群 ,这个需要配置在http里面的server外面,也就是和 server同级别
upstream windowsapp.com{ server 127.0.0.1:8050 weight=1; server 127.0.0.1:8060 weight=1; }
3. location 配置 proxy
location / { proxy_pass http://windowsapp.com; }
这时候,你访问 http://localhost:2016,有时候会访问的是 8050,有时候是 8060的网站了。
你修改完毕 conf文件后可能要重启,打开cmd
cd c:\nginx-1.9.9
nginx -s reload
执行完毕就Ok了。
静态文件缓存配置
1. 把 CSS 和 js以及图片拷贝到 c:\nginx-1.9.9\staticfiles
2. 在sever里面添加如下配置,重启 nginx,请注意下面是 root,
也就是说, scripts/jquery.js 会请求 /nginx-1.9.9/staticfiles/scripts/jquery.js,亲,小心哦。
你可以随便修改,配置一个错误的,你就会发现 js和css就请求不到了。
location ~ \.(css)$ { root /nginx-1.9.9/staticfiles/; expires 7d; } location ~ \.(js)$ { root /nginx-1.9.9/staticfiles/; expires 7d; }
这时候,你就已经缓存 js和css文件了,时间是7d。
参考:
Nginx搭建反向代理服务器过程详解:http://www.open-open.com/lib/view/open1417488526633.html
Nginx静态文件处理:http://m.blog.csdn.net/blog/kun20031029/25398049
Simple Load Balancing: https://www.nginx.com/resources/wiki/start/topics/examples/loadbalanceexample/
nginx 集群报"upstream" directive is not allowed here 错:
http://www.jiucool.org/blog/nginx-cluster-reported-upstream-directive-is-not-allowed-here-error/
标签:
原文地址:http://www.cnblogs.com/mouse_in_beijing/p/5060083.html