点赞
评论
收藏
分享
举报
nginx系列之四:web服务器
发表于2020-09-03 16:40

浏览 1.3k

前言

nginx系列之一:nginx入门
nginx系列之二:配置文件解读
nginx系列之三:日志配置
nginx系列之四:web服务器
nginx系列之五: 负载均衡
nginx系列之六:cache服务
nginx系列之七:限流配置
nginx系列之八:使用upsync模块实现负载均衡

转自:在此感谢原博主的整理分享

一、nginx 做静态服务器

HTML页面如下

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<h1>图片展示</h1>

<div>
    <img src="/static/images/1.png">
</div>
</body>
</html>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

上传相关文件,生成如下路径

tree html/
html/
├── index.html
└── static
    └── images
        └── 1.png
## 配置nginx.conf 配置文件
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}
/data/app/nginx/sbin/nginx -t 
nginx: the configuration file /data/app/nginx-1.10.3/conf/nginx.conf syntax is ok
nginx: configuration file /data/app/nginx-1.10.3/conf/nginx.conf test is successful
/data/app/nginx/sbin/nginx -s reload    
  • 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

浏览器访问:
在这里插入图片描述

这个时候我们可以把static静态页面给拆分出来

worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
        }
        location /static/ {
            root /data/db;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}
  • 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

将静态文件迁移到/data/db目录下,并重启nginx服务。

mv html/static/ /data/db/
/data/app/nginx/sbin/nginx -t 
/data/app/nginx/sbin/nginx -s reload  
  • 1
  • 2
  • 3

测试图片是否能否获取:

curl -I http://192.168.56.12/static/images/1.png
HTTP/1.1 200 OK
Server: nginx/1.10.3
Date: Sun, 08 Apr 2018 09:31:35 GMT
Content-Type: image/png
Content-Length: 32239
Last-Modified: Sun, 08 Apr 2018 09:21:26 GMT
Connection: keep-alive
ETag: "5ac9df16-7def"
Accept-Ranges: bytes
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

1.1 对图片开启gzip压缩

worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;

    gzip on;
    gzip_min_length 1k;
    gzip_buffers    4 16k;
    gzip_http_version 1.1;
    gzip_comp_level 6;
    gzip_types image/png;
    gzip_vary on;
    
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
        }
        location /static/ {
            root /data/db;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}
/data/app/nginx/sbin/nginx -t 
/data/app/nginx/sbin/nginx -s reload  
  • 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

对比两次响应头信息,开启gzip 压缩后响应头多了Content-Encoding: gzip,开启压缩成功。
在这里插入图片描述
在这里插入图片描述

二、nginx 反向代理后端服务器

2.1 配置nginx环境

user  www www;
worker_processes 8;
error_log  /data/logs/nginx_error.log  crit;
pid        /usr/local/webserver/nginx/nginx.pid;
#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 65535;
 
events{
  use epoll;
  worker_connections 65535;
}

http{
  include       mime.types;
  default_type  application/octet-stream;
  #charset  gb2312;
  server_names_hash_bucket_size 128;
  client_header_buffer_size 32k;
  large_client_header_buffers 4 32k;
  client_max_body_size 8m;
  sendfile on;
  tcp_nopush     on;
  keepalive_timeout 60;
  tcp_nodelay on;
include  gzip.conf;
include blog.biglittle.cn.conf;
  • 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

gzip.conf文件内容

  gzip on;
  gzip_min_length  1k;
  gzip_buffers     4 16k;
  gzip_http_version 1.0;
  gzip_comp_level 2;
  gzip_types       text/plain application/x-javascript text/css application/xml;
  gzip_vary on;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

blog.biglittle.cn.conf文件内容

##  server
  {
    listen 80 default;
    server_name blog.biglittleant.cn;
    index index.html index.htm index.php;
    root html;
    location ~ .*\.(php|php5)?$
    {
      fastcgi_pass  127.0.0.1:9000;
      fastcgi_index index.php;
      include fastcgi.conf;
    }
  }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

fastcgi.conf文件内容

  fastcgi_connect_timeout 300;
  fastcgi_send_timeout 300;
  fastcgi_read_timeout 300;
  fastcgi_buffer_size 64k;
  fastcgi_buffers 4 64k;
  fastcgi_busy_buffers_size 128k;
  fastcgi_temp_file_write_size 128k;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

2.2 安装PHP 环境

下载PHP文件,并安装基础依赖包

wget http://cn2.php.net/distributions/php-7.1.2.tar.gz
 yum -y install  libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel libcurl-devel libjpeg-turbo-devel openssl openssl-devel
  • 1
  • 2

编译安装

./configure --prefix=/data/app/php-7.1.2 --enable-fpm --with-fpm-user=nginx --with-fpm-group=nginx --with-mysqli --with-zlib --with-curl --with-gd --with-jpeg-dir --with-png-dir --with-freetype-dir --with-openssl --enable-mbstring --enable-xml --enable-session --enable-ftp --enable-pdo -enable-tokenizer --enable-zip --enable-bcmath --enable-sockets --with-gettext
make && make install 
ln -s /data/app/php-7.1.2/ /data/app/php7
cp php.ini-development /data/app/php7/lib/php.ini
cp sapi/fpm/php-fpm.service /usr/lib/systemd/system/
  • 1
  • 2
  • 3
  • 4
  • 5

修改配置文件

vim /data/app/php7/lib/php.ini
# 查找 mysqli.default_socket,修改成:
mysqli.default_socket = /data/app/mysql/mysql.sock
date.timezone = PRC
  • 1
  • 2
  • 3
  • 4

好了,PHP 7 已经安装好,下面验证一下

shell > /data/app/php7/bin/php -v
PHP 7.0.5 (cli) (built: Apr  8 2016 00:08:04) ( NTS )
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies
  • 1
  • 2
  • 3
  • 4

再查看下已经安装的模块

/data/app/php7/bin/php -m
  • 1

接着配置 php-fpm文件

# copy php-fpm 的配置文档
cp /data/app/php7/etc/php-fpm.conf.default /data/app/php7/etc/php-fpm.conf
cp /data/app/php7/etc/php-fpm.d/www.conf.default /data/app/php7/etc/php-fpm.d/www.conf
  • 1
  • 2
  • 3

其中 www.conf 中要留意以下这个值 listen = 127.0.0.1:9000

配置 php-fpm 启动服务脚本

修改启动脚本,把里边 prefix 相关的内容用实际路径代替

vim /usr/lib/systemd/system/php-fpm.service
PIDFile=/usr/local/php7/var/run/php-fpm.pid
ExecStart=/usr/local/php7/sbin/php-fpm --nodaemonize --fpm-config /usr/local/php7/etc/php-fpm.conf
# 重新载入 systemd
systemctl daemon-reload
systemctl start php-fpm 
ss -lntup |grep 9000
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

2.3 编写PHP测试文件

vim /data/app/nginx/html/hello.php 编写一个PHP测试文件。

<html>
 <head>
  <title>PHP 测试</title>
 </head>
 <body>
 <?php phpinfo(); ?>
 </body>
</html>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

2.4 测试是否可用

/data/app/nginx/sbin/nginx -t 
/data/app/nginx/sbin/nginx -s reload  
  • 1
  • 2

在这里插入图片描述


已修改于2023-03-08 05:09
创作不易,留下一份鼓励
守望

暂无个人介绍

关注



写下您的评论
发表评论
全部评论(0)

按点赞数排序

按时间排序

关于作者
守望
这家伙很懒还未留下介绍~
89
文章
0
问答
17
粉丝
相关文章
介绍nginx网页配置工具QQ技术交流群1:1106758598QQ技术交流群2:560797506邮箱: cym1102@qq.com官网地址: http://www.nginxwebui.cn码云: https://gitee.com/cym1102/nginxWebUIgithub: https://github.com/cym1102/nginxWebUI功能特点nginxWebUI也可管理多个nginx服务器集群,随时一键切换到对应服务器上进行nginx配置,也可以一键将某台服务器配置同步到其他服务器,方便集群管理.部署此项目后,配置nginx再也不用上网各种搜索配置代码,再也不用手动申请和配置ssl证书,只需要在本项目中进行增删改查就可方便的配置和启动nginx。技术说明本项目是基于springBoot的web系统,数据库使用sqlite,因此服务器上不需要安装任何数据库项目启动时会释放一个.sqlite.db到系统用户文件夹中,注意进行备份本系统通过Let'sencrypt申请证书,使用acme.sh脚本
点赞 6
浏览 6.1k
  前三周学习了陶辉老师的“NGINX基础培训系列课程”,感觉受益良多,在这里想把一些知识点记录一下,和大家分享一下知识点,也方便日后的随手查看,温故知新。  首先,我们了解到了Nginx的版本,Nginx发布版本分为主线版本和稳定版本,区分两个版本也非常简单,主线版本版本号为单数,比如1.19,稳定版本为双数,比如1.18,今天我要说的是稳定版本,这个版本会尽量少的减少Nginx的bug问题,适用于生产环境,这里我不建议使用Nginx和其他软件一样在生产环境中落后一个或多个大版本使用,之前生产环境做过漏扫,发现我们编译自带的Nginx版本为:nginx/1.13.3(查询命令为nginx-V),结果出现了多个漏洞,四个高危和一个中危漏洞:        通过升级Nginx到稳定版最新版本后修复!  其次,是Nginx发行版本的选择,目前比较流行的有:nginx、nginxplus、Tengine、openresty、ope
点赞 1
浏览 3.2k
感谢您参加“NGINX从入门到精通进阶系列培训”!以下为培训的问答、课件和录像,希望您能通过此培训学有所得,祝学习进步!>问与答:- 基础篇+高级篇 - 应用篇+实战篇(New)>课件(PPT):基础篇:-NGINX概要、安装、配置:https://interact.f5.com/rs/653-SMC-783/images/CNFEB22-NginxCoreCourse-Setup.pdf-NGINX日志、运维:https://interact.f5.com/rs/653-SMC-783/images/cnfeb22-nginxcorecourse-maintenance.pdf高级篇:-NGINX变量、API:https://interact.f5.com/rs/653-SMC-783/images/CNFEB22-NginxCoreCourse-API.pdf-NGINXSSL、NJS:https://interact.f5.com/rs/653-SMC-783/images/CNFEB22-NginxCoreCourse-SSL.pdf
点赞 10
浏览 4.8k