ngnix介绍及使用

mac2024-06-06  60

1. centos 一键安装nginx环境

$ yum install nginx 根据提示 进行确认 下一步 即可安装完毕; 服务器默认根目录为 : /usr/share/nginx/html. nginx 配置目录为 :/etc/nginx/nginx.conf. nginx操作命令: 启动 service nginx start 停止 service nginx stop 重启 service nginx restart

2. linux安装ngnix

1. 安装ngnix的编译环境. 参考ngnix安装手册.doc 2. 把ngnix的代码上传到linux. 3. 解压代码 tar -zxvf ngnix-1.8.0.tar.gz 4. 注意:上边将临时文件目录指定为/var/temp/nginx,需要在/var下创建temp及nginx目录 5. 配置makefile 参考安装手册 configure 参数设置, 复制执行一遍就会出现makefile 6. 编译 执行 make 命令 7. 安装 执行 make install ,成功后安装的目录为configure参数设置的--prefix=/usr/local/nginx 下面. 8. 查看 cd /usr/local/nginx/sbin 下 是否有可执行文件 ngnix 或者 安装nginx [root@localhost]tar zxvf nginx-1.8.0.tar.gz [root@localhost] cd nginx-1.8.0 [root@localhost] ./configure && make && make install

3. docker 安装 ngnix

Dockerfile 文件: FROM nginx:1.15-alpine COPY cms-page.conf /etc/nginx/conf.d/default.conf WORKDIR /app COPY ./dist /app cms-page.conf 文件: server { listen 8080; location ^~/apis { rewrite ^~/apis/(.*)$ /$1 break; proxy_pass https://www.xiangchuxing.cn:8089; } location / { root /app; try_files $uri $uri/ /index.html; index index.html; } location ~ (.*\.json) { root /app; error_page 405 =200 $1; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } docker-compose.yml 文件: version: '3' services: cms-nginx: image: "nginx:1.15-alpine" container_name: cms-nginx networks: - nginx_nw ports: - "8080:8080" cms-page: build: registry.git.brightcns.cn/official-website/cms-page:master-5720 container_name: cms-page depends_on: - cms-nginx networks: nginx_nw: driver: bridge

4. ngnix应用场景

1. http服务器.Nginx是一个http服务可以独立提供http服务.可以做网页静态服务器 2. 虚拟主机。可以实现在一台服务器虚拟出多个网站.例如个人网站使用的虚拟主机. 3. 反向代理,负载均衡
nginx代理示例
server { listen 80; server_name localhost; location / { root /home/images/; index index.html index.htm; } }

5. ngnix实现虚拟主机配置

可以实现在同一台服务器上实现多个网站,而且网站间互相不干扰. 同一个服务器可能有一个ip,网站需要80端口.网站的域名不同. 区分不同网站有3种方式: 1). ip区分 2). 端口区分 3). 域名区分
5.1基于ip的虚拟主机配置
1. nginx的配置文件 #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 { #一个Server就是一个虚拟主机 listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { root html; index index.html index.htm; } } } 2. 基于ip的虚拟主机配置 , 一个server就是一个虚拟主机,在配置文件添加server server { listen 80; server_name 192.168.25.141; #charset koi8-r; #access_log logs/host.access.log main; location / { root html-141; index index.html index.htm; } } server { listen 80; server_name 192.168.25.100; #charset koi8-r; #access_log logs/host.access.log main; location / { root html-100; index index.html index.htm; } } 3. nginx重新加载配置文件 ./ngnix -s reload
5.2 基于端口的虚拟主机配置
注: 首先关闭防火墙,原因若81,82 端口不开放会访问不到. server { listen 81; server_name 192.168.25.141; #charset koi8-r; #access_log logs/host.access.log main; location / { root html-81; index index.html index.htm; } } server { listen 82; server_name 192.168.25.141; #charset koi8-r; #access_log logs/host.access.log main; location / { root html-82; index index.html index.htm; } }
5.3 基于域名的虚拟主机
注: 都用80端口,即ip相同,端口相同,区分域名(最常用). 一个域名只能绑定一个ip地址,一个ip地址可以被多个域名绑定. 添加基于域名虚拟主机的配置: server { listen 80; server_name www.itheima.com; #charset koi8-r; #access_log logs/host.access.log main; location / { root html-www; index index.html index.htm; } } server { listen 80; server_name hehe.itheima.com; #charset koi8-r; #access_log logs/host.access.log main; location / { root html-hehe; index index.html index.htm; } }

6. ngnix反向代理

nginx只做请求的转发,后台有多个http服务器提供服务, nginx的功能就是把请求转发给后面的服务器,决定把请求转发给谁. nginx的反向代理配置: upstream tomcatserver1 { server 192.168.25.141:8080; } upstream tomcatserver2 { server 192.168.25.141:8081; } server { listen 80; server_name 8080.itheima.com; #charset koi8-r; #access_log logs/host.access.log main; location / { proxy_pass http://tomcatserver1; index index.html index.htm; } } server { listen 80; server_name 8081.itheima.com; #charset koi8-r; #access_log logs/host.access.log main; location / { proxy_pass http://tomcatserver2; index index.html index.htm; } } 如果在同一个域名下有多台服务器提供服务,此时需要nginx负载均衡.

7. nginx负载均衡

7.1 负载均衡原理
1. 负载均衡 建立在现有网络结构之上,它提供了一种廉价有效透明的方法扩展网络设备 和服务器的带宽、增加吞吐量、加强网络数据处理能力、提高网络的灵活性和可用性. 2. 负载均衡,英文名称为Load Balance,其意思就是分摊到多个操作单元上进行执行, 例如Web服务器、FTP服务器、企业关键应用服务器和其它关键任务服务器等, 从而共同完成工作任务.
7.2 负载均衡需求
nginx作为负载均衡服务器,用户请求先到达nginx,再由nginx根据负载配置将请求转发至 tomcat服务器。 nginx负载均衡服务器:192.168.25.141 tomcat1服务器:192.168.25.141:8080 tomcat2服务器:192.168.25.141:8081

8. ngnix高可用

解决高可用的方案就是添加冗余。(添加备份机) 通过 keepalived 对主备nginx实时心跳检测 (keepalived nginx) 参考 keepalived nginx 文档.

本文由博客一文多发平台 OpenWrite 发布!

最新回复(0)