1.常用命令
启动
./nginx
检查 nginx.conf配置文件
./nginx -t
重启
./nginx -s reload
停止
./nginx -s stop
2.配置文件示例
#反向代理配置文件示例一
server {
listen 80;
server_name 192.168.31.205;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
proxy_pass http://127.0.0.1:8080;
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;
}
#反向代理配置文件示例二
server {
listen 9001;
server_name 192.168.31.205;
location ~ /stu/ {
proxy_pass http://127.0.0.1:8080;
}
location ~ /edu/ {
proxy_pass http://127.0.0.1:8081;
}
}
#负载均衡配置文件示例
upstream myserver{
server 192.168.31.205:8080;
server 192.168.31.205:8081;
}
server {
listen 80;
server_name 192.168.31.205;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
proxy_pass http://myserver;
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;
}
3.反向代理和正向代理的区别
正向代理是一个位于客户端和目标服务器之间的代理服务器(中间服务器)。为了从原始服务器取得内容,客户端向代理服务器发送一个请求,并且指定目标服务器,之后代理向目标服务器转交并且将获得的内容返回给客户端。正向代理的情况下客户端必须要进行一些特别的设置才能使用(例如访问谷歌的工具)。
反向代理正好相反。对于客户端来说,反向代理就好像目标服务器。并且客户端不需要进行任何设置。客户端向反向代理发送请求,接着反向代理判断请求走向何处,并将请求转交给客户端,使得这些内容就好似他自己一样,一次客户端并不会感知到反向代理后面的服务,也因此不需要客户端做任何设置,只需要把反向代理服务器当成真正的服务器就好了。