快速入门

  1. 安装
  2. 常用命令
  3. 配置文件

安装

使用源码安装

1、 安装环境

`yum -y install make zlib zlib-devel gcc gcc-c++ libtool openssl openssl-devel`

2、 使用wget下载源码包

`wget http://downloads.sourceforge.net/project/pcre/pcre/8.35/pcre-8.35.tar.gz`
`wget http://nginx.org/download/nginx-1.12.2.tar.gz`

3、安装pcre

  • 解压pcre源码包:tar -xvf pcre-8.35.tar.gz
  • 进入到pcre解压缩包
  • 执行命令./configure
  • 执行命令make && make install
  • 查看版本,确认安装成功pcre-config --version

4、安装nginx

  • 解压nginx压缩包
  • 进入到nginx压缩包
  • 执行./configure
  • 执行make && make install
  • 查看版本:/usr/local/webserver/nginx/sbin/nginx -v

使用docker安装

使用docker安装,至于docker怎么安装去看docker


不使用docker的话,可以使用源码安装,或者yum安装

常用命令

1、源码安装

执行/usr/local/nginx/sbin目录下的nginx可执行程序

/usr/local/nginx/sbin:启动
/usr/local/nginx/sbin -s stop:关闭
/usr/local/nginx/sbin -s reload:更改配置文件之后的热部署

2、yum安装

使用系统服务命令来启动和停止

service nginx start:启动
service nginx stop:停止
service nginx restart:重启

配置文件

1、源码安装位置 /usr/local/nginx/conf/nginx.conf

2、yum安装位置 /etc/nginx/nginx.conf

3、即使不确定,也可以使用命令nginx -t来查看配置文件的位置


我们将这个配置文件拷贝到主机上 docker cp 3516d6423546:/etc/nginx /etc/nginx

我曾经想要使用容器数据卷来挂载,但是会出现docker闪退的情况

于是我使用交互方式进去,之后发现/etc/nginx什么文件都没有了

解决这问题也很简单,只需要先启动一个容器,将配置文件先拷贝到主机上,然后重启另一个容器挂载数据卷即可


nginx.conf

# 全局块
# 主要会设置一些影响nginx服务整体运行的配置指令
# 主要包括:1、配置运行nginx服务器的用户(组);2、允许生成的worker processes数(处理并发的数量);3、进程pid存放路径;4、日志存放路径;5、类型和配置文件的引入
user  nginx;
worker_processes  1;

# error_log:错误日志的存放路径
error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;

# ---------------------------------------------------------------------





# events块
# 主要配置nginx服务器与用户的网络连接
# 常用包括:1、是否开启对多worker_processes下的网络连接进行序列化;2、是否允许同时接收多个网络连接;3、选取哪种事件驱动模型来处理连接请求;4、每个worker_processes可以同时支持的最大连接数
# worker_connections  1024:这个意思就是nginx的worker_processes的最大连接数是1024
events {
    worker_connections  1024;
}

# ---------------------------------------------------------------------

# http块
# nginx中配置最频繁的部分
# http块又包括两个内容:http全局快和server块
http {
    
    # include是个主模块指令,作用是包含路径中的文件
    include       /etc/nginx/mime.types;
    
    # http块的核心指令
    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;
}

我们可以看到,对于http块的server部分只有一个include语句,这是因为引入了这个路径下的文件,http块会拿出来讲


/etc.nginx/conf.d/default.conf:那个目录下只有这一个文件

server {
	
	# 监听的端口号
    listen       80;
    # 监听的端口号的ipv6版本,如果需要改变端口号,这里也需要该
    listen  [::]:80;
    
    # 主机名称,因为是本地所以是localhost
    server_name  localhost;

    #charset koi8-r;
    #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;
    #}
}

转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。