1.介绍

Nginx是一款轻量级的Web服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器。其特点是占有内存少,并发能力强,事实上nginx的并发能力在同类型的网页服务器中表现较好,中国大陆使用nginx的网站有:百度、京东、新浪、网易、腾讯、淘宝等。

Nginx是由伊戈尔·赛索耶夫为俄罗斯访问量第二的Rambler.ru站点(俄文:Рамблер)开发的,第一个公开版本0.1.0发布于2004年10月4日。

官网:https://nginx.org/

2.Nginx-命令(手动安装限定)

1.查看版本

./nginx -v

2.检查配置文件

./nginx -v

3.启动

./nginx

4.停止

./nginx -s stop

停止之后,我们可以查看nginx的进程:

ps -ef|grep nginx

5.重新加载

当修改了Nginx配置文件后,需要重新加载才能生效,可以使用下面命令重新加载配置文件:

./nginx -s reload

3.环境变量配置

在上述我们在使用nginx命令在进行服务的启动、停止、重新加载时,都需要用到一个指令nginx,而这个指令是在nginx/sbin目录下的,我们每一次使用这个指令都需要切换到sbin目录才可以,使用相对繁琐。那么我们能不能在任意目录下都可以执行该指令来操作nginx呢?答案是可以的,配置nginx的环境变量即可。

打开/etc/profile文件, 在PATH环境变量中增加nginx的sbin目录,如下:

ecpot PATH=/usr/local/nginx/sbin:$JAVA_HOME/bin:$MAVEN_HOME/bin:$PATH

修改完配置文件之后,需要执行 source /etc/profile 使文件生效。 接下来,我们就可以在任意目录下执行nginx的指令了

#安装

手动安装

1.安装依赖包

由于nginx是基于c语言开发的,所以需要安装c语言的编译环境,及正则表达式库等第三方依赖库。

yum -y install gcc pcre-devel zlib-devel openssl openssl-devel

2.下载Nginx安装包

以1.16.1版本为例

yum install wget
wget https://nginx.org/download/nginx-1.16.1.tar.gz

wget:

wget命令用来从指定的URL下载文件。wget非常稳定,它在带宽很窄的情况下和不稳定网络中有很强的适应性,如果是由于网络的原因下载失败,wget会不断的尝试,直到整个文件下载完毕。如果是服务器打断下载过程,它会再次联到服务器上从停止的地方继续下载。

3.解压nginx压缩包

tar -zxvf nginx-1.16.1.tar.gz

4.配置Nginx编译环境

cd nginx-1.16.1
./configure --prefix=/usr/local/nginx

----prefix 指定的目录,就是我们安装Nginx的目录。

5.编译&安装

make & make install

docker安装

1.docker拉取nginx镜像

docker pull nginx

2.创建映射容器的文件目录

创建配置文件目录

mkdir -p /mydata/nginx/conf/
mkdir -p /mydata/nginx/conf.d/
mkdir -p /mydata/nginx/log/

授予权限

chmod 777 /mydata/nginx/conf/
chmod 777 /mydata/nginx/conf.d/
chmod 777 /mydata/nginx/log/

3.在/mydata/nginx/conf/中创建nginx.conf文件

user  nginx;
worker_processes  auto;
error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;
events {
    worker_connections  1024;
}
http {
    include       /etc/nginx/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  /var/log/nginx/access.log  main;
    sendfile        on;
    #tcp_nopush     on;
    keepalive_timeout  65;
    #gzip  on;
    include /etc/nginx/conf.d/*.conf;
}

4.在/mydata/nginx/conf.d/中创建default.conf文件

server {
    listen       80;
    listen  [::]:80;
    server_name  localhost;
    #access_log  /var/log/nginx/host.access.log  main;
    location / {
        root   /usr/share/nginx/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   /usr/share/nginx/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;
    #}
}

5.创建容器

docker run -p 80:80 --name nginx \
--restart=always \
-v /mydata/nginx/conf/nginx.conf:/etc/nginx/nginx.conf \
-v /mydata/nginx/conf.d/default.conf:/etc/nginx/conf.d/default.conf \
-v /mydata/nginx/log:/var/log/nginx \
-d nginx

-d:后台运行

-p:端口映射

--name:指定容器名称

-v:数据卷映射

--restart:重启容器方式

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。