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>

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

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

浏览器访问:

这个时候我们可以把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;
}
}
}

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

mv html/static/ /data/db/
/data/app/nginx/sbin/nginx -t
/data/app/nginx/sbin/nginx -s reload

测试图片是否能否获取:

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

对图片开启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

对比两次响应头信息,开启gzip 压缩后响应头多了Content-Encoding: gzip,开启压缩成功。

nginx 反向代理后端服务器

配置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;

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;

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;
}
}

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;

安装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

编译安装

./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/

修改配置文件

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

好了,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

再查看下已经安装的模块

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

接着配置 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

其中 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

编写PHP测试文件

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

<html>
<head>
<title>PHP 测试</title>
</head>
<body>
<?php echo '<p>Hello World</p>'; ?>
<?php phpinfo(); ?>
</body>
</html>

测试是否可用

/data/app/nginx/sbin/nginx -t
/data/app/nginx/sbin/nginx -s reload

死磕nginx系列--nginx服务器做web服务器的更多相关文章

  1. 死磕nginx系列--nginx 目录

    死磕nginx系列--nginx入门 死磕nginx系列--nginx配置文件 死磕nginx系统-nginx日志配置 死磕nginx系列--nginx服务器做web服务器 死磕nginx系列--使用 ...

  2. 树莓派做web服务器(nginx、Apache)

    一想到Linux Web服务器,我们首先想到的是: Apache + MySql + Php. Apache:是世界使用排名第一的Web服务器软件. 可以运行在几乎所有广泛使用的计算机平台上,由于其跨 ...

  3. Nginx是什么,有什么优点?为什么选择Nginx做web服务器软件?(经典经典)

    1.基础知识 代理服务器:    一般是指局域网内部的机器通过代理服务器发送请求到互联网上的服务器,代理服务器一般作用在客户端.应用比如:GoAgent,FQ神器.    一个完整的代理请求过程为:客 ...

  4. Ubuntu+Django+Nginx+uWSGI+Mysql搭建Python Web服务器

    Ubuntu+Django+Nginx+uWSGI+Mysql搭建Python Web服务器 闲着无聊的时候部署了一个Django项目玩,用vm虚拟机部署的. 准备工作 我使用的系统是Ubuntu16 ...

  5. Nginx是如何配置为 Web 服务器的【转载】

    详解 Nginx是如何配置为 Web 服务器的 林涛 发表于:2016-11-29 23:23 分类:WebServer 标签:Nginx,web,web服务器 521次 抽象来说,将 Nginx 配 ...

  6. ubuntu12.04 安装nginx+php+mysql (lnmp)的web服务器环境

    1.Ubuntu12.04 安装nginx+php+mysql (lnmp)的web服务器环境 http://blog.db89.org/ubuntu12-04-install-nginx-php-m ...

  7. 一生挚友redo log、binlog《死磕MySQL系列 二》

    系列文章 原来一条select语句在MySQL是这样执行的<死磕MySQL系列 一> 一生挚友redo log.binlog<死磕MySQL系列 二> 前言 咔咔闲谈 上期根据 ...

  8. 打开order by的大门,一探究竟《死磕MySQL系列 十二》

    在日常开发工作中,你一定会经常遇到要根据指定字段进行排序的需求. 这时,你的SQL语句类似这样. select id,phone,code from evt_sms where phone like  ...

  9. 重重封锁,让你一条数据都拿不到《死磕MySQL系列 十三》

    在开发中有遇到很简单的SQL却执行的非常慢,甚至只查询一行数据. 咔咔遇到的只有两种情况,一种是MySQL服务器CPU占用率很高,所有的SQL都执行的很慢直到超时,程序也直接502,另一种情况是行锁造 ...

随机推荐

  1. Mac 自带的Apache php 狼神的

    开启服务:sudo /usr/sbin/apachectl start 停止服务:sudo /usr/sbin/apachectl stop 重启服务:sudo /usr/sbin/apachectl ...

  2. .net 多线程的使用(Thread)

    上篇 net 同步异步 中篇 多线程的使用(Thread) 下篇 net 任务工厂实现异步多线程 Thread多线程概述 上一篇我们介绍了net 的同步与异步,我们异步演示的时候使用的是委托多线程来实 ...

  3. 【转载】Nginx+Tomcat 动静分离实现负载均衡

    0.前期准备 使用Debian环境.安装Nginx(默认安装),一个web项目,安装tomcat(默认安装)等. 1.一份Nginx.conf配置文件 1 # 定义Nginx运行的用户 和 用户组 如 ...

  4. MySQL,Oracle,PostgreSQL,mongoDB,Hive, SAP HANA 数据库web维护客户端管理工具

    TreeDMS数据库管理系统使用JAVA开发,采用稳定通用的springMVC +JDBC架构,实现基于WEB方式对 MySQL,Oracle,PostgreSQL,mongoDB ,Hive, SA ...

  5. php解释命令行的参数

    php cli模式下,可以用$argc, $argv来读取所有的参数以及个数,如: ghostwu@ghostwu:~/php/php1/1$ cat go1 #!/usr/bin/php <? ...

  6. Can’t connect to local MySQL server through socket的解决方法

    http://www.aiezu.com/db/mysql_cant_connect_through_socket.html mysql,mysqldump,php连接mysql服务常会提示下面错误: ...

  7. 微信小程序传参数的几种方法

    1,navigator 跳转时 wxml页面(参数多时可用“&”) <navigator url='../index/index?id=1&name=aaa'></n ...

  8. 通过ssh实现远程登陆服务器!

    通过ssh实现远程登陆前提是服务器已经开启ssh服务,至于怎么开启,可以参看上一篇“Linux服务器开启ssh服务,实现ssh远程登陆!”! 使用ssh登陆时,输入主机(linux的ip地址),账号, ...

  9. 从零开始学习html(十二)CSS布局模型——上

    一.css布局模型 清楚了CSS 盒模型的基本概念. 盒模型类型, 我们就可以深入探讨网页布局的基本模型了. 布局模型与盒模型一样都是 CSS 最基本. 最核心的概念. 但布局模型是建立在盒模型基础之 ...

  10. Postman Postman测试接口之JSON结构化数据提交

    Postman测试接口之JSON结构化数据提交   by:授客 QQ:1033553122 本文主要是针对结构比较复杂一点的JSON协议数据的提交做个简单说明 举例: 用户下订单接口 接口方向 客户端 ...