nginx web服务器概念了解 配置
服务器
服务器
服务器是一种提供高效计算的机器,与普通的PC主机相比,具有可观的稳定性,高并发性,可扩展性。
互联网任何一个应用都是以服务器为基础设施的,没有服务器我们就无法访问网络上的任何内容,只能使用单机的应用。例如网站,我们访问的任何一个网站都是保存在某个服务器上的,域名被DNS(域名解析服务器)解析到IP地址后,浏览器就能通过IP地址访问对应的服务器资源了。
就好比:服务器是人的家,人名相当于域名(不可重名),身份证号相当于IP地址。通过人名搜索到身份证号,通过身份证号获取到家的地址。
Web服务器
Web服务器不再是一种硬件设施,而是一种部署在服务器上的软件应用,它服务于各种网络请求,将网络请求进行处理,分发。
所以Web服务器的处理能力很大程度决定了该网站的并发能力。著名的Web服务器有:Apache Nginx
Web应用服务器
Web应用服务器是专门处理逻辑代码的服务器,同时还具有了处理网络请求的能力,一般为了提高并发能力,会在Web应用服务器上套一层Web服务器。
例如:Tomcat uwsgi gunicorn,后两个是Python的Web应用服务器,专门处理Python的逻辑代码。
联系
其中Web服务器和Web应用服务器都部署在服务器上。
Nginx服务器
Nginx (engine x) 是一个高性能的HTTP和反向代理web服务器,同时也提供了IMAP/POP3/SMTP服务。其主要特点如下:
- 轻量级 并发能力强
- 支持处理静态资源,以减少应用服务器的压力
- 负载均衡
负载均衡
大型的网站应用网站应用往往是由无数个服务器服务的,就像淘宝这种,单靠一个是不可能承受的了如此大的并发量,因此有了负载均衡。负载均衡又分为硬负载均衡和软负载均衡,硬负载均衡是通过硬件的方式实现负载均衡,比如F5,成本都比较昂贵,软负载均衡则是通过软件的方式实现,比如Nginx和Apache。
所谓负载均衡就是将多个请求分发到不同的服务器上去,每个服务器都有处理请求和逻辑的应用服务器,以此来提高并发量。
下面使用Nginx来实现负载均衡配置
配置Nginx需要到/etc/nginx/nginx.conf文件内进行编辑
http {
##http的配置
server {
listen 80;//监听端口
server_name 域名;
location / {
proxy_pass http://lca;
}
}
upstream lca {//采用轮询方式,依次将请求转发到各个服务器
server 192.168.1.1:5000;
server 192.168.1.2:5000;
server 192.168.1.3:5000;
}
}
上面是采用轮询的方式实现端口转发 负载均衡,还有几种方式实现:
- 权重方式:指定每个服务的权重比例,weight和访问比率成正比,通常用于后端服务机器性能不统一,将性能好的分配权重高来发挥服务器最大性能.
upstream lca {
server 192.168.1.1:5000 weight=1;
server 192.168.1.2:5000 weight=2;
server 192.168.1.3:5000 weight=3;
}
- iphash
每个请求都根据访问ip的hash结果分配,经过这样的处理,每个用户固定访问一个后端服务。
upstream lca {//权重与iphash结合
ip_hash
server 192.168.1.1:5000 weight=1;
server 192.168.1.2:5000 weight=2;
server 192.168.1.3:5000 weight=3;
}
解决跨域问题
跨域请求问题
为了提高浏览器的安全性,引入了跨域限制,也就是同源策略。
所谓源:如果两个页面(接口)的协议,端口或者域名都相同,那么两个页面就有相同的源。如果在同一个页面访问不同源的资源,则会出现500错误。
- 浏览器从一个域名的网页去请求另一个域名的资源时,域名、端口、协议任一不同,都是跨域
- 跨域限制主要是为了安全考虑
前端在请求后端资源时,往往会出现错误代码500的情况。
nginx解决跨域问题
解决跨域问题的方式有很多,在这里介绍通过nginx来解决跨域问题。
上面说到客户端浏览器同时请求服务器的不同资源若不是同源就会引发同源策略问题,那要是服务器请求服务器呢?那么照这个思路就想到了nginx的反向代理。
我们可以使用nginx的反向代理,将不同源的服务器集中到一个nginx web服务器下,也就是通过nginx来反向代理各个服务器。
server
{
listen 80;
server_name cola666.top;
// =/ 表示精确匹配路径为/的url,真实访问为http://localhost:8000
location = / {
proxy_pass http://localhost:8000;
}
//当匹配到/a的url自动去localhost:8000
location /a{
proxy_pass http://localhost:8001;
}
location /baidu/ {
proxy_pass http://www.baidu.com/;
}
}
- 当有请求www.cola666.top的资源时,服务器接收到,会自动将请求内容交给localhost:8000web服务器处理。
- 当有请求www.cola666.top/a下的资源时,服务器接收到,会自动将请求内容交给localhost:8001web服务器处理。
- 当有请求www.cola666.top/baidu/下的资源时,服务器接收到,会请求百度的服务器资源。
虽然请求同源url,但实际上nginx帮助我们转发到其他web服务器,所以实际上访问的是非同源url资源,以实现跨域问题!
location匹配规则
- 当一个url匹配到多个location时,nginx将请求转发给匹配最长的location来处理
- 代理
location /b/ {
proxy_pass http://www.baidu.com/;
}
location /b/ {
proxy_pass http://www.baidu.com;
}
二者的区别为后者会将location中的/b/也添加进url中,比如,后者则代理到http://www.baidu.com/b/xxx,前者则是http:///www.baidu.com/xxx
下面为一个比较简易的完整的nginx配置
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
location / {
proxy_pass http://localhost:8080;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
最后附加一个全面的nginx配置,包括静态资源缓存,负载均衡,Https,等等
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
#自定义的日志格式
log_format main '[($remote_addr) - ($remote_user [$time_local]) $request" '
'($status) '
'($http_user_agent)($http_x_forwarded_for)'
'($upstream_addr) ($upstream_response_time) ($request_time) ]';
proxy_cache_path /data/nginx/tmp-test levels=1:2 keys_zone=tmp-test:100m inactive=7d max_size=10g;
access_log /var/log/nginx/access.log main;
gzip on;
gzip_min_length 1k;
gzip_buffers 16 64k;
gzip_http_version 1.1;
gzip_comp_level 4;
gzip_types text/plain application/javascript application/x-javascript text/javascript text/xml text/css;
gzip_vary on;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
include /etc/nginx/conf.d/*.conf;
#allow to access by the same origin
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Credentials' 'true';
upstream zzm {
server localhost:8000;
#server locahost:8001;
}
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location /static{
alias /var/static;
}
location /{
proxy_cache tmp-test;
proxy_cache_key $uri;
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Headers "Origin, X-Requested-With, Content-Type, Accept";
add_header Access-Control-Allow-Methods "GET, POST, OPTIONS";
#include uwsgi_params;
proxy_pass http://zzm;
# if django be used socket model to start up,using uwsgi of the following
#uwsgi_pass 127.0.0.1:8000;
#uwsgi_read_timeout 180;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header REMOTE_ADDR $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
real_ip_recursive on;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
#配置https
server {
listen 443 ssl http2 default_server;
listen [::]:443 ssl http2 default_server;
server_name cola666.top;
root /usr/share/nginx/html;
ssl_certificate /var/xxx.pem;#ssl两个证书路径
ssl_certificate_key /var/xxx.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 10m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
#静态资源路径
location /static{
alias /var/static;
}
location / {
#缓存路径
proxy_cache tmp-test;
proxy_cache_key $uri;
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Headers "Origin, X-Requested-With, Content-Type, Accept";
add_header Access-Control-Allow-Methods "GET, POST, OPTIONS";
#include uwsgi_params;
#uwsgi_pass 127.0.0.1:8000;
proxy_pass http://zzm;
proxy_redirect off;
#将客户端ip地址交给服务器后端
proxy_set_header Host $host;
proxy_set_header REMOTE_ADDR $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
real_ip_recursive on;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
}
```
nginx web服务器概念了解 配置的更多相关文章
- 20步打造最安全的NGINX WEB服务器
Nginx 是一个轻量级的,高性能的Web服务器以及反向代理和邮箱(IMAP/POP3)代理服务器.它运行在UNIX,GNU /linux,BSD 各种版本,Mac OS X,Solaris和Wind ...
- 构建高效安全的Nginx Web服务器
一 为什么选择Nginx搭建Web服务器 Apache和Nginx是目前使用最火的两种Web服务器,Apache出现比Nginx早.Apache HTTP Server(简称Apache)是世界使用排 ...
- 烂泥:Windows下安装与配置Nginx web服务器
本文由秀依林枫提供友情赞助,首发于烂泥行天下. 前几篇文章,我们使用nginx都是在linux环境下,今天由于工作的需要.需要在windows环境也使用nginx搭建web服务器. 下面记录下有关ng ...
- nginx web服务器详解1(转)
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://freeloda.blog.51cto.com/2033581/1285332 大 ...
- nginx web服务器应用
Nginx介绍 Nginx是一个开源的,支持高性能,高并发的www服务和代理服务软件,因具有高并发(特别是静态资源),占用系统资源少等特性,且功能丰富而逐渐流行起来.功能应用上,Nginx不但是一个优 ...
- Nginx web 服务器 安装篇
Nginx介绍: 静态web服务器有Nginx .Apache .lighttpd等 目前国内用的最常见的就是Nginx 和Apache 是一个开源的.支持高性能.高并发的www服务和代理服务软件,N ...
- nginx web服务器的安装使用
nginx是一个web服务器(高性能web服务器),类似于apache服务器和iis服务器,由于nginx服务器具有轻量级高并发的特点,目前nginx的使用已经超越了apache. nginx介绍:n ...
- WEB服务器(IIS)的配置与管理
安装Web服务器(IIS) 在"服务器管理器"-"角色"-"添加角色"-选择"Web服务器(IIS)"进行安装 这里,我 ...
- 08 部署nginx web服务器(转发fastDFS请求)
先准备两个文件: fastdfs-nginx-module-master.tar.gz:fastDFS nginx安装包 nginx-1.17.3.tar.gz:nginx安装包 注:这两个包文件要匹 ...
随机推荐
- IC-二进制, 自然数, 有符号数
https://mp.weixin.qq.com/s/9rGs9yN-OvLvEclnDZr87Q 零. 概念和结论 1. 数:概念名,指代物体的多少,即物体的量的概念: 2. 值:概念属性,数的 ...
- Protocol Buffers工作原理
这里记录一下学习与使用Protocol Buffer的笔记,优点缺点如何使用这里不再叙述,重点关注与理解Protocol Buffers的工作原理,其大概实现. 我们经常使用Protocol Buff ...
- Java实现 LeetCode 639解码方法 2(递推)
639. 解码方法 2 一条包含字母 A-Z 的消息通过以下的方式进行了编码: 'A' -> 1 'B' -> 2 ... 'Z' -> 26 除了上述的条件以外,现在加密字符串可以 ...
- Java实现 LeetCode 257 二叉树的所有路径
257. 二叉树的所有路径 给定一个二叉树,返回所有从根节点到叶子节点的路径. 说明: 叶子节点是指没有子节点的节点. 示例: 输入: 1 / \ 2 3 \ 5 输出: ["1->2 ...
- 将XML转换为JSON并强制数组
string xml = @"<person id='1'> <name>Alan</name> <url>http://www.google ...
- 120行代码打造.netcore生产力工具-小而美的后台异步组件
相信绝大部分开发者都接触过用户注册的流程,通常情况下大概的流程如下所示: 接收用户提交注册信息 持久化注册信息(数据库+redis) 发送注册成功短信(邮件) 写操作日志(可选) 伪代码如下: pub ...
- 如何在宝塔上的Nginx实现负载均衡
创建一个指向服务器本身的localhost站点(127.0.0.1)和一个指向服务器的站点,域名和IP都可以. I.对域名站点配置: upstream myproj { server 127.0.0 ...
- 《刻意练习之C#》-0016- C#预处理器指令
预处理指令 这些指令/命令不会转换为可执行代码,但会影响编译过程的各个方面:列如,可以让编译器不编译某一部分代码等. C#中主要的预处理指令 #define和#undef #define指令定义: # ...
- easypoi 读取 Excel 简单应用
背景 在做接口测试的时候,经常会使用 Excel 来存储对应的接口信息和用例信息,为了方便程序的读取,引入easypoi 工具来读取 Excel 内容.easypoi 比起 poi 使用更加的方便,代 ...
- [51nod 1847]奇怪的数学题
[ 51nod 1847 ]奇怪的数学题 题目 点这里看题目. 分析 是挺奇怪的...... 以下定义质数集合为\(P\),\(p_i\)为第\(i\)个质数. 定义\(mp(x)\) ...