项目实训-智能生物序列分析平台-前端模块(2)

本文详细介绍了如何在阿里云服务器上部署前端静态网页,并通过Nginx进行配置,使其对外提供服务。讲解了Nginx的安装、基本配置结构以及常用命令。还涉及了错误排查、防火墙端口开放和负载均衡的设置,为全网访问提供了基础。
摘要由CSDN通过智能技术生成

在使用前端静态网页部署到具体公网IP对应的服务器中,然后暴露接口后,可以使得在全网上进行访问。下面是具体的实现方式与代码分析

1.首先我们需要有一台具有公网ip地址的服务器,我所选的是阿里云的2核4g的服务器,带宽为5M,正常部署网站足够使用。

2.在这台服务器中进行nginx的安装,本nginx是在Ubuntu20.04系统下运行的
nginx version: nginx/1.18.0 (Ubuntu)

安装:

1.安装nginx的方法直接apt install nginx就可以
我们下面来看详细配置情况
当我们安装完nginx以后,可以在/etc/nginx中找到其配置情况
nginx.conf是它的配置文件,其中引入了confd.d/下的所有.conf文件
然后它的站点配置信息由sites-enabled/default这个文件配置,拿到这个站点文件之后就可以在里面生命server{}的站点配置
2.还有另外一种安装方式,先将它的安装打包文件传到服务器中,然后解压后进入文件./configure &&make&& make install
下面有几条相关的命令
启动:nginx 或者 nginx -c /配置文件的路径

  停止: nginx -s stop

  重启: nginx -s reload

  检查配置文件: nginx -t

  查看nginx启动情况: ps -ef | grep nginx

如果出现报错

nginx: [error] invalid PID number "" in "/run/nginx.pid"

需要先执行

nginx -c /etc/nginx/nginx.conf

nginx.conf文件的路径可以从nginx -t的返回中找到。

nginx -s reload

如果以上没用,则直接kill调nginx里面的进程

配置文件

注:nginx的配置文件在安装后的nginx目录下的conf文件夹中


(一):刚安装好的配置文件具体如下:

#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 {
        listen       80;
        server_name  localhost;
 
        #charset koi8-r;
 
        #access_log  logs/host.access.log  main;
 
        location / {
            root   html;
            index  index.html index.htm;
        }
 
        #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;
        #}
    }
 
 
    # 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;
    #    }
    #}
 
}

上述是由安装包(第二种方法安装后的配置文件)
用第一种安装后的配置文件更加解耦化,有多个文件include而成,上诉有解释。配置相关差不多。

(二)配置详解

1、根据上面的nginx配置文件,可以将nginx的配置分为以下的组成结构

... #全局快
events {  #events快
}
http{ #http块
    server { #server快
        location { #location快
        }
    }
}

2、每块的结构功能

  (1)、全局块: 配置影响nginx全局的指令。一般有运行nginx服务器的用户组,nginx进程pid存放路径,日志存放路径,配置文件引入,允许生成worker process数等。

  (2)、events块: 配置影响nginx服务器或与用户的网络连接。有每个进程的最大连接数,选取哪种事件驱动模型处理连接请求,是否允许同时接受多个网路连接,  开启多个网络连接序列化等

  (3)、http块: 可以配置多个server,配置代理、缓存、日志定义等能和第三方模块的配置。如文件引入,mime-type定义,日志自定义,是否使用sendfile传输文件,连接超时时间,单连接请求数等。 

  (4)、server块: 配置虚拟主机的相关参数,一个http中可以有多个server。

  (5)、location块: 配置请求的路由,以及各种页面的处理情况。

3、nginx配置文件详细的解释

########### 每个指令必须有分号结束。#################
#user administrator administrators;  #配置用户或者组,默认为nobody nobody。
#worker_processes 2;  #允许生成的进程数,默认为1
#pid /nginx/pid/nginx.pid;   #指定nginx进程运行文件存放地址
error_log log/error.log debug;  #制定日志路径,级别。这个设置可以放入全局块,http块,server块,级别以此为:debug|info|notice|warn|error|crit|alert|emerg
events {
    accept_mutex on;   #设置网路连接序列化,防止惊群现象发生,默认为on
    multi_accept on;  #设置一个进程是否同时接受多个网络连接,默认为off
    #use epoll;      #事件驱动模型,select|poll|kqueue|epoll|resig|/dev/poll|eventport
    worker_connections  1024;    #最大连接数,默认为512
}
http {
    include       mime.types;   #文件扩展名与文件类型映射表
    default_type  application/octet-stream; #默认文件类型,默认为text/plain
    #access_log off; #取消服务日志    
    log_format myFormat '$remote_addr–$remote_user [$time_local] $request $status $body_bytes_sent $http_referer $http_user_agent $http_x_forwarded_for'; #自定义格式
    access_log log/access.log myFormat;  #combined为日志格式的默认值
    sendfile on;   #允许sendfile方式传输文件,默认为off,可以在http块,server块,location块。
    sendfile_max_chunk 100k;  #每个进程每次调用传输数量不能大于设定的值,默认为0,即不设上限。
    keepalive_timeout 65;  #连接超时时间,默认为75s,可以在http,server,location块。
 
    upstream mysvr {   
      server 127.0.0.1:7878;
      server 192.168.10.121:3333 backup;  #热备
    }
    error_page 404 https://www.baidu.com; #错误页
    server {
        keepalive_requests 120; #单连接请求上限次数。
        listen       4545;   #监听端口
        server_name  127.0.0.1;   #监听地址       
        location  ~*^.+$ {       #请求的url过滤,正则匹配,~为区分大小写,~*为不区分大小写。
           #root path;  #根目录
           #index vv.txt;  #设置默认页
           proxy_pass  http://mysvr;  #请求转向mysvr 定义的服务器列表
           deny 127.0.0.1;  #拒绝的ip
           allow 172.18.5.54; #允许的ip           
        } 
    }
}

以上都是apt安装的包,后面介绍一下,下载安装包的nginx的使用配置,下面是一些在配置过程中出现并且了解的知识点 

 在配置使用命令行进行的文件结构中
etc/nginx下面有两个主要引入的配置文件:
1.第一个是conf.d中的文件
2.第二个是site-enables中的文件,其中site-enable的文件就是对site-avalible中的文件的链接,直接对site-avalible中的文件进行修改就可以
在初始化的时候,conf.d中是没有default文件的,需要自己写相关的配置文件,这个的格式可以copy site-enables中的文件。之前一直在徘徊的问题也是这个,使用的格式一直不对。
 

#
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# https://www.nginx.com/resources/wiki/start/
# https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/
# https://wiki.debian.org/Nginx/DirectoryStructure
#
# In most cases, administrators will remove this file from sites-enabled/ and
# leave it as reference inside of sites-available where it will continue to be
# updated by the nginx packaging team.
#
# This file will automatically load configuration files provided by other
# applications, such as Drupal or Wordpress. These applications will be made
# available underneath a path with that package name, such as /drupal8.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##
 
# Default server configuration
#
server {
        listen 80 default_server;
        listen [::]:80 default_server;
        # SSL configuration
        #
        # listen 443 ssl default_server;
        # listen [::]:443 ssl default_server;
        #
        # Note: You should disable gzip for SSL traffic.
        # See: https://bugs.debian.org/773332
        #
        # Read up on ssl_ciphers to ensure a secure configuration.
        # See: https://bugs.debian.org/765782
        #
        # Self signed certs generated by the ssl-cert package
        # Don't use them in a production server!
        #
        # include snippets/snakeoil.conf;
 
        #这里很关键,要定在文件渲染之后对目录中,后面要求对location是/,以后直接用地址加端口号就可以访问了
        root /home/web/hexo/public;
          # Add index.php to the list if you are using PHP
        index index.html index.htm index.nginx-debian.html;
 
        server_name **.**.***.**;
 
        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ =404;
        }
  # pass PHP scripts to FastCGI server
        #
        #location ~ \.php$ {
        #       include snippets/fastcgi-php.conf;
        #
        #       # With php-fpm (or other unix sockets):
        #       fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
        #       # With php-cgi (or other tcp sockets):
        #       fastcgi_pass 127.0.0.1:9000;
        #}
 
        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #       deny all;
        #}
}

如果不使用如下的情况应该可以直接在nginx中进行配置
这样直接地址加端口就能映射到整个文件了

防火墙相关代码

在linux中访问nginx,默认是不能访问的,因为防火墙问题,我们需要开放一部分端口号
查看开放的端口号:
开启防火墙
systemctl start firewalld
关闭防火墙
systemctl stop firewalld
查看端口开放情况
firewall-cmd –list-ports
开放某个特定的端口
firewall-cmd –zone=public –add-port=81/tcp –permanent
关闭某个特定的端口
firewall-cmd –remove-port=80/tcp –permanent
更改配置后重启防火墙
systemctl reload firewalld

负载均衡

1.准备工作
(1)准备两台 tomcat 服务器,一台 8080,一台 8081
(2)在两台 tomcat 里面 webapps 目录中,创建名称是 edu 文件夹,在 edu 文件夹中创建 页面 a.html,用于测试
2.在 nginx 的配置文件中进行负载均衡的配置

upstream myserver {
    server ***.***.***.***:port1
    server ***.***.***.***:port2
}
 
server {
    listen 80
    server_name ********
 
    location / {
        proxy_pass http://myserver;
        root 
        index 
    }
}

3.nginx 分配服务器策略
第一种 轮询(默认)
每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器 down 掉,能自动剔除。
第二种 weight
weight 代表权重默认为 1,权重越高被分配的客户端越多
第三种 ip_hash
每个请求按访问 ip 的 hash 结果分配,这样每个访客固定访问一个后端服务器
第四种 fair(第三方) 按后端服务器的响应时间来分配请求,响应时间短的优先分配

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值