Nginx的概念是什么

52次阅读
没有评论

共计 6559 个字符,预计需要花费 17 分钟才能阅读完成。

本篇内容主要讲解“Nginx 的概念是什么”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让丸趣 TV 小编来带大家学习“Nginx 的概念是什么”吧!

1. Nginx 的概念?

「什么是 Nginx 呢:」

Nginx 是一个基于 HTTP 的反向代理服务器,也是一个基 IMAP/POP3/SMTP 服务邮件服务器

反向代理服务器:现在我们 A 需要访问的目标 B 服务器的 10.7.182.100,我要访问这个 B 服务器上的资源,现在如果使用了 Nginx 之后,我可以通过 Nginx 服务器从而达到访问 10.7.182.100 这个服务器的目的

IMAP/POP/SMTP:这三个是邮件的传输协议

邮件服务器:发送邮件 接收邮件

Web 服务器:本身是一个 Web 服务器的软件,类似于 Tomcat 这种 Web 服务的软件

「Nginx 能干什么呢:」

可以作为 Web 服务器

可以作为邮件服务器

可以作为反向代理的服务器

动静分离 (就是将动态资源和静态资源分隔开)

可以实现负载均衡

2、Nginx 的安装

「Nginx 安装步骤:」

 第一步: 下载我们的 nginx  这里以 1.6.2 版本为例   第二步: 共享安装文件到我们的 linux 上面   第三步: 将文件拷贝到 /usr/local 下面   第四步: 安装  tar -zxvf xxxx.tar.gz  第五步: 下载所需要的依赖库  yum install pcre yum install pcre-devel yum install zlib yum install zlib-devel  第六步: 进行 config 的配置  cd nginx-1.6.2   ./configure --prefix=/usr/local/nginx  第七步: 安装  make   make install  第八步: 启动 nginx /usr/local/nginx/sbin/nginx  关闭: .... -s stop -s reload  查看端口是否有问题  netstat -tunpl |grep 80  浏览器进行验证没问题就可以 

3、Nginx 的配置文件的解析

「配置文件:」

#user nobody; #工作的线程 (4 核 8 线程那么下面就设置成 8   物理硬件有关) worker_processes 1; #全局的错误日志存放的地方  #error_log logs/error.log; #全局错误日志存放的地方   后面的 notice 表示的是输出错误日志的格式  #error_log logs/error.log notice; #error_log logs/error.log info; #nginx 进程号存放的地方  #pid logs/nginx.pid; #最大的连接数 (这个也跟硬件是有关系的) events { worker_connections 1024; } #下面就是跟 HTTP 请求有关系的了  http { #表示的是当前服务器支持的类型  include mime.types; #默认传输的数据类型是流  default_type application/octet-stream; # 声明了一种日志格式   这种日志格式的名字叫做  main log_format main  $remote_addr - $remote_user [$time_local]  $request     $status $body_bytes_sent  $http_referer     $http_user_agent   $http_x_forwarded_for  #表示的是每一次请求的日志记录   格式就是上面的 main 格式  access_log logs/access.log main; #是否可以发送文件  sendfile on; #tcp_nopush on; #这个是双活的超时的时间  #keepalive_timeout 0; keepalive_timeout 65; #是否打开文件的压缩  #gzip on; #虚拟一个主机  #负载均衡的配置  upstream qianyu { ip_hash; server 10.7.182.110:8080; server 10.7.182.87:8080; } #server { # listen 90; # server_name localhost; # location / { # root qianyu; # index qianyu.html; # } #做一个反向代理  #表示的是   如果你访问的后缀是  .jpg 结尾的话那么   就访问下面的另外的服务器  # location ~ \.jpg$ { # proxy_pass http://10.7.182.110:8080; # } # 下面要演示一个负载均衡的例子  #location ~ \.jpg$ { # proxy_pass http://qianyu; #} #} #虚拟一个动静分离的机器  server { listen 9999; server_name localhost; #表示的是动态资源访问的机器  location / { proxy_pass http://10.7.182.54:8080; } #表示的是非  css 和 js 文件访问的地址  location ~ .*\.(htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma)$ { root /usr/local/webapp; expires 30d; } #表示的是 css 和 js 的访问地址  location ~ .*\.(js|css)?$ { root /usr/local/webapp; expires 1h; } } #虚拟了服务器  server { #端口是  80 listen 80; #这个表示的是虚拟服务器默认访问的是本机  server_name localhost; #这个是虚拟服务器的编码  #charset koi8-r; #当前服务器的日志的存储的地方  #access_log logs/host.access.log main; #做了一个地址的映射  location / { root html; index index.html index.htm; } #如果报 404 的时候访问的页面  #error_page 404 /404.html; # redirect server error pages to the static page /50x.html #报错 50 开头的是   就访问 50x.html error_page 500 502 503 504 /50x.html; # 下面对 50x.html 又做了一个映射   就访问根目录下的 html 中的  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; #} } # 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; # } #} }

4、实现 Nginx 下的反向代理

「做一个反向代理:」

表示的是如果你访问的后缀是 .jpg 结尾的话,那么就访问下面的另外的服务器

location ~ \.jpg$ { proxy_pass http://10.7.182.110:8080; }

5、实现 Nginx 下的负载均衡

「第一种策略:默认是轮循的策略:」

 配置 upstream upstream qianyu { server 10.7.182.110:8080; server 10.7.182.87:8080; }  配置负载均衡  location ~ \.jpg$ { proxy_pass http://qianyu; }

「第二种策略:权重(weight):」

# 负载均衡的配置  upstream qianyu { server 10.7.182.110:8080 weight=2; server 10.7.182.87:8080 weight=1; }  配置负载均衡  location ~ \.jpg$ { proxy_pass http://qianyu; }

「第三种策略:IPHash 的使用:」

 # 负载均衡的配置  upstream qianyu { ip_hash; server 10.7.182.110:8080; server 10.7.182.87:8080; }  配置负载均衡  location ~ \.jpg$ { proxy_pass http://qianyu; }

6、实现 Nginx 下的动静分离

「动静分离:」

简单的来说就是将动态资源和静态资源给分隔开

静态资源放到 Nginx 服务器上

动态资源放到 Tomcat 服务器上

Jpg html css png gif .... 静态资源  ---- 放到 Nginx 服务器上  .action 结尾的都是动态的资源  ------------------------- 放到 Tomcat 的服务器上 

「动静分离的实现:」

在 /usr/local 目录下创建 webapp 文件夹

在 webapp 目录下创建 css、html、js、img 文件夹

编写 HTML 的内容并将 html 文件放到 webapp 目录下

!DOCTYPE html  

html  

  head  

  meta charset= utf-8  /  

  title /title  

  link rel= stylesheet  type= text/css  href= /css/main.css /  

  script src= /js/main.js  type= text/javascript  charset= utf-8 /script  

  /head  

  body  

  img src= /img/bbb.jpg /  

  br /  

  a href= /Dynamic_Resource/qianyu.action 点击我访问动态资源 /a  

 

  /body  

/html  


将图片放到 img 目录下,将 css 放到 css 目录下,将 js 文件放到 js 的目录下

编写动态资源的这个工程

package com.qy.servlet; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class qianyuServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType( text/html;charset=utf-8  PrintWriter writer=response.getWriter(); writer.write(this is dynamic resource test  writer.flush(); writer.close(); } }

编写配置文件    /conf/nginx.xml 文件

#虚拟一个动静分离的机器  server { listen 9999; server_name localhost; #表示的是动态资源访问的机器  location / { proxy_pass http://10.7.182.54:8080; } #表示的是非  css 和 js 文件访问的地址  location ~ .*\.(htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma)$ { root /usr/local/webapp; expires 30d; } # 表示的是 css 和 js 的访问地址  location ~ .*\.(js|css)?$ { root /usr/local/webapp; expires 1h;

测试

7、虚拟主机

「虚拟主机配置:」

server { listen 90; server_name localhost; location / { root qianyu; index qianyu.html; } #做一个反向代理  # 表示的是   如果你访问的后缀是  .jpg 结尾的话那么   就访问下面的另外的服务器  location ~ \.jpg$ { proxy_pass http://10.7.182.110:8080; } }

到此,相信大家对“Nginx 的概念是什么”有了更深的了解,不妨来实际操作一番吧!这里是丸趣 TV 网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

正文完
 
丸趣
版权声明:本站原创文章,由 丸趣 2023-08-17发表,共计6559字。
转载说明:除特殊说明外本站除技术相关以外文章皆由网络搜集发布,转载请注明出处。
评论(没有评论)