这篇文章主要讲解了“Nginx的原理和作用是什么”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Nginx的原理和作用是什么”吧!
Nginx是一款轻量级的Web服务器,反向代理服务器及电子邮件代理服务器。
反向代理:是指以代理服务器来接受internet上的连接请求,然后将请求转发给内部网络上的服务器,并将从服务器上得到的结果返回给internet上请求连接的客户端,此时代理服务器对外就表现为一个反向代理服务器。
nginx常用命令:
1、nginx -s stop:快速关闭nginx,可能不保存相关信息,并迅速终止web服务
2、nginx -s quit:平稳关闭nginx,保存相关信息,有安排的结束web服务
3、nginx -s reload:重新打开日志文件
4、nginx -c filename :为nginx指定一个配置文件,来代替缺省的
5、nginx -t :不运行,而仅仅测试配置文件,nginx将检查配置文件的语法的正确性,并尝试打开配置文件中所引用到的文件
6、nginx -v:显示nginx的版本。
nginx主要作用:http反向代理配置,负载均衡配置,网站有多个webapp的配置,静态站点配置,跨域解决方案。
http反向代理实现:
#运行用户
#user nobody;
#启动进程,通常设置成和cpu的数量相等
worker_processes 1;
#全局错误日志
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#PID文件,记录当前启动的nginx的进程ID
#pid logs/nginx.pid;
#工作模式及连接上限
events {
worker_connections 1024;#单个后台work process进程的最大并发连接数
}
#设定http服务器,利用它的反向代理功能提供负载均衡支持
http {
#设定mime类型(邮件支持类型),类型有mime.type文件定义
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指令指定nginx是否调用sendfile函数来输出文件,对于普通应用,必须设置为 on
#如果用来进行下载等应用磁盘IO重载应用,可设置为off,以平衡磁盘与网络I/O处理速度,降低系统等uptime
sendfile on;
#tcp_nopush on;
#连接超时时间
#keepalive_timeout 0;
keepalive_timeout 65;
#压缩开关
#gzip on;
#设定实际的服务器列表
Upstream zp_server1{
server 127.0.0.1:8089
}
#HTTP服务器
server{
#监听80端口,80端口时知名端口号,用于HTTP协议
listen 80;
#定义使用www.XX.com访问
server_name www.xuecheng.com;
#指向webapp的目录
#root
#编码格式
charset utf-8;
ssi on;
ssi_silent_errors on;
location / {
alias F:/teach/xcEdu/xcEduUI01/xc-ui-pc-static-portal/;
#首页
index index.html;
}
}
# 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;
# }
#}
}
负载均衡配置:网站在实际运营过程中,多半都是有多台服务器同时运行着同样的app,这时需要使用负载均和来分流
#运行用户
#user nobody;
#启动进程,通常设置成和cpu的数量相等
worker_processes 1;
#全局错误日志
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#PID文件,记录当前启动的nginx的进程ID
#pid logs/nginx.pid;
#工作模式及连接上限
events {
worker_connections 1024;#单个后台work process进程的最大并发连接数
}
#设定http服务器,利用它的反向代理功能提供负载均衡支持
http {
#设定mime类型(邮件支持类型),类型有mime.type文件定义
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指令指定nginx是否调用sendfile函数来输出文件,对于普通应用,必须设置为 on
#如果用来进行下载等应用磁盘IO重载应用,可设置为off,以平衡磁盘与网络I/O处理速度,降低系统等uptime
sendfile on;
#tcp_nopush on;
#连接超时时间
#keepalive_timeout 0;
keepalive_timeout 65;
#压缩开关
#gzip on;
#设定实际的服务器列表
upstream zp_server1{
#weight参数表示权重,权重越高,被分配到的几率越大
server 127.0.0.1:8089 weight=5;
server 127.0.0.1:8088 weight=1;
server 127.0.0.1:8087 weight=6;
}
#HTTP服务器
server{
#监听80端口,80端口时知名端口号,用于HTTP协议
listen 80;
#定义使用www.XX.com访问
server_name www.xuecheng.com;
#指向webapp的目录
#root
#编码格式
charset utf-8;
#打开SSI
ssi on;
ssi_silent_errors on;
location / {
proxy_pass http://zp_server1
#首页
index index.html;
}
}
# 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;
# }
#}
}
网站有多个webapp的配置:当一个网站功能越来越丰富时,往往需要将一些功能相对独立的模块剥离出来,独立维护。这样的话,通常会有多个webapp。
http{ upstream product_server{ server www.helloworld.com:8081; } upstream admin_server{ server www.helloworld.com:8082; } upstream finance_server{ server www.helloworld.com:8083; } } server{ location / { proxy_pass http://product_server } location /product/ { proxy_pass http://product_server } location /admin/ { proxy_pass http://admin_server } location /finance/ { proxy_pass http://finance_server } }
https反向代理配置:一些对安全性要求比较高的站点,可能会使用HTTPS,使用nginx配置https需要知道几点:
1、HTTPS的固定端口号443,不同于HTTP的80端口
2、SSL标准需要引入安全证书,所以在nginx.conf中你需要指定证书和它对应的key
静态站点配置:
server{ location / { root /app/dist/; index index.html }
跨域解决方案:
1、CORS
在后端服务器设置HTTP响应头,把你需要运行访问的域名加入。
2、jsonp
nginx根据第一种思路,也提供了一种结局跨域的解决方案
首先:在enable-cors.conf文件中设置cors
接着:在你的服务器中include-enable-cors.conf 来引入跨域配置。
感谢各位的阅读,以上就是“Nginx的原理和作用是什么”的内容了,经过本文的学习后,相信大家对Nginx的原理和作用是什么这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。
原文链接:https://my.oschina.net/u/4131739/blog/3122662