nginx(四) nginx用法实例
本人常用的机器是windows,下面在windows环境下演示下Nginx的用法
一、代理后端接口:如我本地在9999端口启动一个服务:
现在使用nginx代理到这个端口:此时访问localhost:9991会代理localhost:9999
通过代理访问接口:localhost:9999/demo/code/getCode使用localhost:9999 = localhost:9991规则来替换,得到localhost:9991/demo/code/getCode
如果Nginx换个写法:
server {
listen 9991;
server_name localhost;
location / {
root html;
index index.html index.htm;
proxy_pass http://localhost:9999/demo/;
}
这时候访问localhost:9991会代理到localhost:9999/demo/,原地址localhost:9999/demo/code/getCod替换下,所以访问代理的url为:
二、代理前端页面:
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
proxy_pass http://localhost:9999;
}
#访问方式:http://localhost/test/或http://localhost/test
location ^~ /test{
alias F:/aaa;
index test.html;
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
ssi on;
ssi_silent_errors on;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
三、代理静态资源:
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
proxy_pass http://localhost:9999;
}
#访问方式:http://localhost/file/或http://localhost/file
location ^~ /file{
alias E:/jdk1.8;
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
ssi on;
ssi_silent_errors on;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
四、使用同一个Nginx配置文件同时代理多个后端服务、静态资源:
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
#访问方式:http://localhost/demo/code/getCode,代理到http://localhost:9999/demo/code/getCode接口
location / {
root html;
index index.html index.htm;
proxy_pass http://localhost:9999;
}
#访问方式:http://localhost/mytest/demo/code/getCode,代理到http://localhost:7777/demo/code/getCode接口
location /mytest/ {
proxy_pass http://localhost:7777/;
}
#访问方式:http://localhost/test/或http://localhost/test
location ^~ /test{
alias F:/aaa;
index test.html;
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
ssi on;
ssi_silent_errors on;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
#访问方式:http://localhost/file/或http://localhost/file
location ^~ /file{
alias E:;
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
ssi on;
ssi_silent_errors on;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
}
五、使用Nginx实现负载均衡:使用upstream。
如同一个服务在本地分别启动9999和7777两个端口:
修改nginx配置并重启:注意weight和=中间不要有空格
这时候访问localhost:9991/demo/code/getCode,可以看到本地9999和7777两个端口在轮询调用。
再如:
upstream mydemoservice{
server localhost:7777 weight=5;
server localhost:9999 weight=5;
}
server {
listen 80;
server_name localhost;
#访问方式:http://localhost/demo/code/getCode,代理到http://localhost:9999/demo/code/getCode接口
location / {
root html;
index index.html index.htm;
proxy_pass http://localhost:9999;
}
location /myserver/ {
proxy_pass http://mydemoservice/;
}
访问localhost/myserver/demo/code/getCode。
还没有评论,来说两句吧...