使用nginx做前端服务器for前端开发人员

使用nginx做前端服务器

介绍

Nginx是一款轻量级高性能的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,其特点是占有内存少,并发能力强

安装

由于在安装nginx的过程中需要g++、gcc、openssl-devel、pcre-devel和zlib-devel的依赖库

所以先安装所需要的依赖库

1)安装依赖

yum install gcc-c++
yum install pcre pcre-devel
yum install zlib zlib-devel
yum install openssl openssl--devel

2) 下载nginx

wget http://nginx.org/download/nginx-1.15.5.tar.gz

3) 解压

解压缩: tar -zxf nginx-1.15.5.tar.gz

         产生一个nginx-1.15.5 目录,进入nginx-1.15.5目录

4)编译和安装

编译:

     ./configure  #默认安装目录是/usr/local

     如果要指定安装目录在后面加上 --prefix=指定目录

安装

    make

    make install

5) 启动

找到我们的 安装目录,如果是默认的目录就是/usr/local/nginx,在nginx目录下有sbin/nginx就是nginx的启动文件,在nginx下执行

启动:
    ./sbin/nginx
停止:
    ./sbin/nginx -s stop
重启:
    ./sbin/nginx -s reload

6)给出一些编译选项,可供选择,一般用不到

--prefix=path           定义一个目录,存放服务器上的文件 ,也就是nginx的安装目录。默认使用 /usr/local/;
--with-pcre=/pcre-8.37      设置PCRE库的源码路径。PCRE库的源码需要从PCRE网站下载并解压。
--with-zlib=/zlib-1.2.8    设置的zlib库的源码路径。要下载从 zlib并解压。
--with-openssl=/openssl-1.0.1t

--without-http_gzip_module    不编译压缩的HTTP服务器的响应模块。编译并运行此模块需要zlib;
--without-http_rewrite_module  不编译重写模块。编译并运行此模块需要PCRE库支持;
--without-http_proxy_module   不编译http_proxy模;
--with-http_ssl_module      使用https协议模块。默认情况下,该模块没有被构建。建立并运行此模块的OpenSSL库是必需的;

7) 贴上三个链接,避免以后再找

pcre: http://ftp.pcre.org/pub/pcre http://ftp.pcre.org/pub/pcre/pcre-8.37.tar.gz 
zlib: http://www.zlib.net/fossils/ http://www.zlib.net/fossils/zlib-1.2.8.tar.gz 
openssl: https://ftp.openssl.org/source http://distfiles.macports.org/openssl/openssl-1.0.1k.tar.gz

8)特殊情况

我们的第一步就是为安装的时候提供的一个编译环境,而我们nginx的运行是不用依赖步骤一的库,如果有一天你有一台无法连接外网的服务器需要你安装nginx,而又恰好上面又没有nginx需要的编译环境,这个时候我们第一想法就是按上编译环境就好了,但是由于其中的某些依赖库在离线状态下特别难搞定(有集成好的一体包的除外),这种情况下我们可以选择跳过编译过程,在另一台可联网设备上把编译好的nginx包打包放到无外网的服务器上(切记nginx目录在两台服务器的位置一定要一模一样,否则会找不到目录)

配置文件

nginx的配置文件在nginx目录下的conf/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;
    #    }
    #}
}

server

一个server监听一个端口,一个nginx可以开多个端口

server {
        listen       80;    #监听的端口
        server_name  localhost;
        location / {    #路径
            root   html;    #根目录
            index  index.html index.htm;  #跟文件
        }
        location /api { 
            rewrite ^/api/(.*)$ /$1 break;  #重定向
            proxy_pass http://127.0.0.1:8080;   #代理
        }
}

项目部署

nginx目录中的html目录是我们放静态文件的位置,比如:我们把official_website放到html下

image.png

我们只需要修改nginx.conf的location即可

location / {
            root html/official_website;
            index index.html index.htm;   
}

常用Linux命令

ls  遍历当前目录下的文件
    -l  遍历当前目录下的文件和目录,并显示文件的详细信息(不包含隐藏文件)
    -a  遍历当前所有文件及目录(包含隐藏文件)
cd 进入某个目录
mkdir 创建一个目录
touch 创建一个文件
cat 查看一个文件的内容
rm 删除
    -r 可删除目录
    -f 跳过确认
pwd 获取当前目录的路径
scp 文件传输
        -r 上传多个文件或目录
    例:
    文件上传:scp file user@IP:path
    文件下载:scp user@IP:filePath path
tar 压缩和解压
    解压:tar -xf filePath.tar
    压缩: tar -cf filePath.tar filePath
vi/vim  常用的文本编辑器
        常用的有两种模式:编辑模式和命令行模式,进去之后默认是命令行模式
    i:进入编辑模式
    esc:进入命令行模式
    在命令行模式下:
    q:退出
    w:保存
    !:强制
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值