Nginx基本配置

1、nginx启动的2种方式

  • 这2种方式不能混用,只能用一种
## 第一种:(适合源码编译安装)
        nginx   启动
        nginx -s stop   停止
        nginx -s reload | restart

## 第二种:(适合yum安装)
        systemctl start nginx   启动
        systemctl stop nginx    停止
        systemctl restart nginx   重启

2、nginx的配置文件

user  nginx;                                                          
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;                # 包含资源类型文件
    default_type  application/octet-stream;             # 默认以下载方式传输给浏览器(前提是该资源在mime.types中无法找到)

    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;        # 以main格式记录日志到该路劲下

    sendfile        on;         # 高效文件传输方式
    #tcp_nopush     on;         

    keepalive_timeout  65;              # 长连接的超时时间

    #gzip  on;          # 是否开启压缩功能

    include /etc/nginx/conf.d/*.conf;           # nginx站点目录
}

3、站点目录配置解析

server {                      # 定义一个网站
    listen       80;            # 监听端口
    server_name  localhost;             # 域名

    #charset koi8-r;    # 字符集
    #access_log  /var/log/nginx/host.access.log  main;          # 日志(若没配置,则http模块中的日志配置生效)

    location / {
        root   /usr/share/nginx/html;           
        # 代码的主文件位置
        # / 代表站点路劲,即 / = /usr/share/nginx/html,若根为 /test,则站点目录为                 /usr/share/nginx/html/test/

        
        index  index.html index.htm;            # 服务端默认返回给用户的文件
        # 用户最终请求到的路径为:/usr/share/nginx/html/index.html
    }           

http server location 扩展了解项

http{} 层下允许有多个Server{}层,一个Server{}层下又允许有多个Location
http{} 标签主要用来解决用户的请求与响应
Server{} 标签主要用来响应具体的某一个网站
Location{} 标签主要用于匹配网站具体的URL路径

4、nginx搭建静态资源web服务器

1、编写nginx配置文件
        [root@nginx conf.d]# cat game.conf 
    server {
        listen 80;
        server_name game.lol.com;

        location / {
            root /code;
            index index.html;
        }

    }

2、根据配置文件,创建目录,上传代码
        创建目录    mkidr /code
        切换到创建的目录        cd /code/
        上传代码    # 使用rz -E 或者手动拖拽
        解压      unzip html5.zip

3、语法检查并重载服务
        nginx -t        # nginx语法检查
        systemctl reload nginx          # 平滑重启

4、配置域名解析
        配置windows上的hosts文件
        10.0.0.61       game.lol.com

重载nginx服务

systemctl restart nginx                      # 立即重启
systemctl reload nginx                  # 平滑重启

5、nginx虚拟主机

nginx配置虚拟主机有如下三方式:

方式一:基于主机多IP方式        (用的很少,不介绍)
方式二:基于端口的配置方式           # 适合在企业内部使用
方式三:基于多个hosts名称方式(多域名方式)  #用的比较多

基于端口的配置方式

1、配置多端口的虚拟主机
        [root@nginx conf.d]# cat port.conf 
        server {
            listen 81;

            location / {
                root /code_81;
                index index.html;
            }
        }

        server {
                listen 82;

                location / {
                        root /code_82;
                        index index.html;
                }
        }
        
2、根据配置文件创建所需的目录
        [root@nginx conf.d]# mkdir /code_8{1..2}
        [root@nginx conf.d]# echo '81' > /code_81/index.html
        [root@nginx conf.d]# echo '82' > /code_82/index.html
        
3、检查语法并重启服务
        [root@nginx conf.d]# nginx -t
    nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
    nginx: configuration file /etc/nginx/nginx.conf test is successful
    [root@nginx conf.d]# systemctl restart nginx
    
4、测试
        [root@nginx conf.d]# curl 10.0.0.60:81
    81
    [root@nginx conf.d]# curl 10.0.0.60:82
    82
    [root@nginx conf.d]# 
    
    或者在浏览器输入:
        10.0.0.60:81    
        10.0.0.60:81

基于域名的配置方式

建议一个域名对应一个.conf文件,不要讲多个域名放在同一个.conf文件中,不方便后期维护

1、准备多虚拟主机配置文件
        [root@nginx conf.d]# cat test1.lol.com.conf 
        server {
            listen 80;
            server_name test1.lol.com;

            location / {
                root /code/test1;
                index index.html;
            }
        }
        [root@nginx conf.d]# cat test2.lol.com.conf 
        server {
            listen 80;
            server_name test2.lol.com;

            location / {
                root /code/test2;
                index index.html;
            }
        }

2、根据配置问价创建对应的目录,并检查语法和重启nginx           # 重启前一定要检查语法
    [root@nginx conf.d]# mkdir /code/test{1..2} -p
    [root@nginx conf.d]# echo "test1_server" > /code/test1/index.html
    [root@nginx conf.d]# echo "test2_server" > /code/test2/index.html
    [root@nginx conf.d]# nginx -t
    nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
    nginx: configuration file /etc/nginx/nginx.conf test is successful
    [root@nginx conf.d]# systemctl restart nginx

3、在windows的hosts文件中配置域名解析
    10.0.0.60 test1.lol.com
    10.0.0.60 test2.lol.com

4、测试,通过浏览器访问域名
    test1.lol.com               # 页面显示结果为:test1_server
    test2.lol.com               # 页面显示结果为:test2_server

PS:
        当我们配置了错误的hosts解析,如:将 test2.lol.com,误写成 test3.lol.com,那么当我们在浏览器输入test3.lol.com 后,浏览器先将域名解析成IP,然后根据IP找到nginx,但在nginx中匹配不到该域名,那么nginx就会将放在最靠前的域名配置虚拟主机返回给浏览器!

6、nginx日志

nginx的日志分为:

​    访问日志 access.log:有优先级,server中配置了就放在server配置的路径中,否则以http层配置的为准

​       错误日志 error.log : 路径 /var/log/nginx/error.log

nginx日志切割配置文件:

​ vim /etc/logrotate.d/nginx

/var/log/nginx/*.log {
        daily
        missingok
        rotate 52
        compress
        delaycompress
        notifempty
        create 640 nginx adm
        sharedscripts
        postrotate
                if [ -f /var/run/nginx.pid ]; then
                        kill -USR1 `cat /var/run/nginx.pid`
                fi
        endscript
}