• 【必读】从零开始,一步步教你安装nginx,搭建个人博客网站!


    nginx搭建个人网站

    Nginx是一款轻量级Web服务器、反向代理服务器以及电子邮件代理服务器,并且具有高并发连接处理能力和低内存消耗的特点。它也可以用于负载均衡和缓存控制等功能。

    功能:

    • 静态网站服务器:Nginx可以用来作为静态网站服务器,支持HTML、CSS、JavaScript等静态文件。
    • 动态网站服务器:通过安装PHP、Python等解释器,Nginx可以作为动态网站服务器,支持动态页面生成。
    • 反向代理服务器:Nginx可以作为反向代理服务器,接收来自客户端的请求,然后将请求转发到后端的服务器上,返回的结果再返回给客户端。
    • 负载均衡:Nginx可以用来作为负载均衡器,将客户端的请求分发到多个后端服务器上,提高网站的并发处理能力。
    • 邮件代理:Nginx可以作为邮件代理服务器,支持IMAP和POP3协议,可以用来接收和转发邮件。

    安装方式

    1、yum 安装 2、rpm 3、源码编译安装 4、docker部署安装

    环境: Centos8

    本次采用yum进行安装,适合新手学习。

    部署

    # 确定服务器可以访问外网
    # 更新yum源
    yum update -y
    
    # 安装
    yum install nginx
    
    # 使用systemctl管理nginx服务
    systemctl start nginx
    
    # 设置开机启动
    systemctl enable nginx
    
    # 查看nginx服务状态
    systemctl status nginx
    
    # 如果防火墙开启的话需要放通端口
    firewall-cmd --zone=public --add-port=80/tcp --permanent
    firewall-cmd --reload
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    nginx配置文件路径:

    # 配置文件一般情况下位于/etc/nginx/nginx.conf,整个目录包含主配置文件nginx.conf和其他配置文件
    root@761e1b942a26:/etc/nginx# ls -la
    total 24
    drwxr-xr-x 1 root root   20 Dec 29  2021 .
    drwxr-xr-x 1 root root   19 Nov 13 04:25 ..
    drwxr-xr-x 1 root root   26 Nov 13 04:25 conf.d          # 配置目录,一般在主配置文件包含
    -rw-r--r-- 1 root root 1007 Dec 28  2021 fastcgi_params
    -rw-r--r-- 1 root root 5349 Dec 28  2021 mime.types      # 网页文本类型配置,一般不需要修改
    lrwxrwxrwx 1 root root   22 Dec 28  2021 modules -> /usr/lib/nginx/modules # nginx模块
    -rw-r--r-- 1 root root  648 Dec 28  2021 nginx.conf      # 主配置文件
    -rw-r--r-- 1 root root  636 Dec 28  2021 scgi_params
    -rw-r--r-- 1 root root  664 Dec 28  2021 uwsgi_params
    
    # 网页服务器主目录一般在/usr/share/nginx,包括html目录以及对应index.html文件等
    root@761e1b942a26:/usr/share/nginx/html# ls
    50x.html  index.html
    
    # 默认的nginx日志目录/var/log/nginx,包括访问日志和错误日志,根据主配置文件中定义
    root@761e1b942a26:/var/log/nginx# ls
    access.log  error.log
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    一般情况下的配置: 通过主配置文件定义web服务器的端口和参数,然后将网站资源文件放到服务器网页目录上即可。

    nginx配置文件示例: 配置文件才能模块方式编写,使用{}进行同一区块分别,使用#进行注释。

    • Main Block: 主区块是一个配置文件的顶层区块,其中包含了对整个Nginx服务器的基础配置,如错误日志文件的位置、PID文件的位置、工作进程的数量等。
    • Events Block: 事件区块用于定义如何处理网络连接和处理请求,如采用哪种工作模型、最大连接数量、是否启用多线程等。
    • Http Block: Http区块是用于定义HTTP协议相关的配置,例如MIME类型、访问控制、错误页面重定向等。
    • Server Block: 服务器区块用于定义特定的虚拟主机或者应用程序,它可以包含子区块如Location Block来更具体地定义URL匹配规则和路由。
    • Upstream Block: 负载均衡器区块用于定义后端服务器集群的相关信息,如集群成员、轮询策略、健康检查等。
    # 定义启动nginx服务器的用户以及进程数量
    user  nginx;
    worker_processes  auto;
    # 定义错误日志和pid路径
    error_log  /var/log/nginx/error.log notice;
    pid        /var/run/nginx.pid;
    
    # 工作进程连接数
    events {
        worker_connections  1024;
    }
    
    # http块定义了包含server块的整体定义,如显示版本号,是否开启压缩,日志格式等
    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 定义了引用指定目录下的配置文件,一般将server块单独定义,一个网站一个server配置文件。
        include /etc/nginx/conf.d/*.conf;
    }
    
    # server块,可以在conf.d目录下新建一个xx.conf的文件,写入内容。
    server {
        listen       80;  # 表示侦听80端口,有几种写法,如果多个ip地址可以指定地址,否则全部侦听
        listen  [::]:80;  # 侦听ipv6
        server_name  localhost;  # 域名,内网可以直接写ip地址,外网有dns可以写认证域名
    	# 单独定义这个server的日志
        #access_log  /var/log/nginx/host.access.log  main;
    	# location定义了网页根目录,以及网页默认文件类型index.html
        location / {
            root   /usr/share/nginx/html;
            index  index.html index.htm;
        }
    	# 定义特定404页面返回文件
        #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;
        }
    	# 配置PHP代理,实现动态网站搭建
        # 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;
        #}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    alt

    本文由 mdnice 多平台发布

  • 相关阅读:
    视频营销终极指南,独立站卖家必看
    国外视频搬运素材去哪里找?可搬运下载国外的素材网站库分享
    【测试开发】node.js下使用 puppeteer 构建截图方案(2/2)
    java-php-python-springboot校园博客系统计算机毕业设计
    (web前端网页制作课作业)使用HTML+CSS制作非物质文化遗产专题网页设计与实现
    重温 JavaScript 系列(4):实现异步的方法、 promise实现文件读取、Promise的并发处理
    Elasticsearch:二进制数据类型 - binary field
    【从头构筑C#知识体系】2.2 匿名方法
    数据结构Map-Set和哈希表
    揭秘报表新玩法!标配插件不再单调,如何用柱形图插件让你的报表瞬间高大上!
  • 原文地址:https://blog.csdn.net/weixin_43483442/article/details/134558390