您的位置:首页 > 运维架构 > Nginx

浅学nginx

2020-06-06 05:43 447 查看

什么是Nginx

nginx 可以作为web服务器,但更多的时候,我们把它作为网关,因为它具备网关必备的功能:

  • 方向代理
  • 负载均衡
  • 动态路由
  • 请求过滤

服务器种类

Web服务器分为2类:

  1. Web应用服务器:
      tomcat
    • resin
    • jetty
  2. Web服务器
      Apache
    • Nginx
    • IIS

区分:Web服务器不能解析JSP等页面,只能处理JS/CSS/html等静态资源。
并发: Web服务器的并发能力远高于Web应用服务器。

nginx方向代理

什么是方向代理?
  • 代理:通过客户机的配置,实现让一台服务器代理客户机,客户机所以有请求都交给代理服务器处理。
  • 反向代理:用一台服务器,代理真实服务器,用户访问时,不再访问真实服务器,而是代理服务器。

nginx可以当做方向代理服务器来使用:

  • 我们需要提前再nginx中配置好反向代理规则,不同请求,交给不同真实服务器处理
  • 请求到达nginx,nginx会根据已经定义的规则进行请求转发,从而实现路由功能

nginx目录结构
1. conf:配置目录
2. contrib:第三方依赖
3. html:默认的静态资源目录,类似于tomcat的webapps
4. logs:日志目录
5. nginx.exe:启动程序。可双击运行,但不建议这么做。

#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;
server{
listen 80;
server_name manage.ishop.com;

proxy_set_header X-Forworded-Host $host;
proxy_set_header X-Forworded-Server $host;
proxy_set_header X-Forworded-For $proxy_add_x_forwarded_for;

location / {
proxy_pass http://127.0.0.1:9001;
proxy_connect_timeout 600;
proxy_read_timeout 600;
}
}
server{
listen 80;
server_name api.ishop.com;

proxy_set_header X-Forworded-Host $host;
proxy_set_header X-Forworded-Server $host;
proxy_set_header X-Forworded-For $proxy_add_x_forwarded_for;

location / {
proxy_pass http://127.0.0.1:9002;
proxy_connect_timeout 600;
proxy_read_timeout 600;
}
}
#server {
#       listen       80;
#	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;//定义50x.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;
#    }
#}
}


nginx 命令
启动nginx start nginx.exe
重启 nginx -s reload
停止 nginx -s stop

SwitchHosts-win32-x64_v3.3.11.5347

SwitchHosts是一个修改、管理、切换多个 hosts 方案的开源工具。
SwitchHosts 除了可以帮助你快速切换不同的 hosts 设置、编辑 hosts 文件外,它还有着一些很不错的特性,比如:

免费、开源、支持三大操作系统
系统托盘快速「一键切换」不同的 hosts 方案
支持 hosts 文件语法高亮,可以方便用户更直观地阅读和修改 Hosts 内容;
编辑 hosts 时,可以点击行号可以对行进行快速注释或取消注释
支持远程 hosts,直接从指定网址读取 hosts 内容,方便多台机器同步 hosts 设置,这是一个很赞的特性!
支持 hosts 配置的导入、导出备份
macOS 系统下可以支持 Alfred workflow 快速切换

基本上,有了 SwitchHosts,你就可以对 hosts 为所欲为了,轻松一键切换毫无鸭梨。而且,远程 hsots 方案也可以非常方便经常换电脑使用的人,配置一次到处使用!这一点也是它的亮点

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: