Nginx分享

xiaoxiao2022-06-03  7

Nginx

 

一、前言

为毛要用nginx服务器代理,为什么不用tomcat?

为毛要使用nginx + tomcat + jdk 开发?

跨域一定要用JSONP或者是CORS(跨域资源共享)?

常见跨域场景

http://www.domain.com/a.js 和 http://www.domain.com/b.js 同一域名,不同文件或路径允许通信

http://www.domain.com:8000/a.js和http://www.domain.com/b.js 同一域名,不同端口 不允许通信

http://www.domian.com/a.js和https://www.domain.com/b.js 同一域名,不同协议 不允许通信

http://www.domain.com/a.js和http://192.168.1.122/b.js 域名和域名对应的IP相同 不允许通信

http://www.domain.com/a.js和http://x.domain.com/b.js 主域相同,子域不同 不允许通信

http://www.domain1.com/a.js和http://www.domain2.com/b.js 不同域名 不允许通信

二、为什么用Nginx?

热部署

nginx具有热部署的功能,可以7 * 24小时不间断服务的前提下升级Nginx的可执行文件。同时也可以在不停止服务的情况下修改配置文件、更换日志文件等功能。

可以高并发链接

这是一个很重要的特性!随着用户量的不断增加,一些公司会出现高并发请求的问题。只要你的内存ok,nginx处理高并发简直是完美。

低内存消耗

在一般的情况下,10000个非活跃的HTTP连接,在nginx仅仅消耗2.5M的内存。

处理响应请求很快

正常情况下单次请求会得到很快的响应。在高峰期,nginx可以比其他的web服务器更快的请求。

前端解决跨域的利器

对于前端而言解决跨域问题更加方便,仅仅需要修改配置文件,就可以完美解决跨域问题。

三、nginx负载均衡原理图

什么叫负载均衡?

负载均衡就是分摊到多个操作单元上进行执行,从而可以共同的完成工作任务。

四、nginx的安装

nginx下载目录

针对不同的操作系统,安装方式也不同,在此我分享一下Windows系统安装nginx的步骤。

下载安装包

解压压缩包

解压后你会看到这样的内容

解释一下解压后的主要内容

conf ---------- nginx的配置文件

html ---------- 前端页面放在这里(需要部署的代码)

logs ---------- 顾名思义,nginx的日志文件

nginx.exe ---------- nginx执行文件(启动nginx)

五、Nginx基本配置

进入到conf文件夹,打开nginx.conf文件,可以看到一下内容

`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; ​ http {   include       mime.types;   default_type application/octet-stream;   server {       listen       80; // 服务侦听80端口       server_name localhost; // 定义使用localhost来访问 ​       #charset koi8-r; ​       #access_log logs/host.access.log main; ​       root   D:/fordearme-box/fordearmebox-web/dist; // 定义服务器的默认网站根目录位置       index index.html index.htm; // 定义首页索引文件的名称 ​ // 当使用history模式的时候需要配置       location / {             try_files $uri $uri/ /index.html;       } ​       #location / { ​       #} ​       location ~ ^/box/wechat/config { // 转发地址           proxy_pass http://39.108.138.254:8082;       } ​       location ~ ^/box/ { // 转发地址           proxy_pass http://39.108.138.254:8081;       } ​       #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;       } ​       # 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;       #}   } }

 

例如后端部署在阿里云服务器上,阿里云的地址是39.108.138.254,悦心盒子接口的端口是8081,当我本地127.0.0.1访问悦心盒子接口时,就需要配置nginx的配置文件;由于接口的前缀都是“/box”开头,那我们可以在配置文件中写入正则,当接口请求到“/box/xxx"的时候就会转发到39.108.138.254的8081端口。

转载请注明原文地址: https://www.6miu.com/read-4915054.html

最新回复(0)