nginx是反向代理服务器,先来理解下正向代理和反向代理,他们对应的角色其实是不同的
正向代理:比如说我想用PC机访问google,介于国内某墙,我们是不可能实现的,此时我们就要绕道国外,寻求一个代理,这个代理可能是某个vpn,也可能直接是自己搭建的vps,最后通过这个代理访问google网站,这里个过程是 PC机器 通过代理,让代理帮我去请求google.com,这就是正向代理,如下图;
反向代理:比如说我们访问百度,这个百度地址的背后可能有上千台服务器为我们服务,具体是哪台,你不知道,也无需知道,你只需要知道这个反向代理的服务器就可以了,这个反向代理服务器会将请求转发到真实的服务器上,这就是反向代理,如下图;
使用windows练习一下nginx
1. 下载nginx:https://nginx.org/en/download.html
2. 解压,进入根目录,修改config/nginx.conf,添加两个server(通常nginx会自带一个80端口的server)
server { listen 8090; 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; #} } server { listen 8091; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { root D:\Tools\webstorm\workspace\element-ui-admin\dist; 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; #} }
3. 双击nginx.exe启动,访问http://localhost:8090/, http://localhost:8091/#/