1
Linux 下玩转 nginx 系列(四)---nginx 做 Web 服务器
作者:anyRTC开发者
- 2022 年 4 月 19 日
本文字数:3363 字
阅读完需:约 11 分钟
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.21.3/conf/nginx.conf syntax is oknginx: configuration file /data/app/nginx-1.21.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.pngHTTP/1.1 200 OKServer: nginx/1.10.3Date: Sun, 08 Apr 2018 09:31:35 GMTContent-Type: image/pngContent-Length: 32239Last-Modified: Sun, 08 Apr 2018 09:21:26 GMTConnection: keep-aliveETag: "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;SHELL 复制 全屏
复制代码
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-gettextmake && make install ln -s /data/app/php-7.1.2/ /data/app/php7cp php.ini-development /data/app/php7/lib/php.inicp 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.sockdate.timezone = PRC
复制代码
好了,PHP 7 已经安装好,下面验证一下
shell > /data/app/php7/bin/php -vPHP 7.0.5 (cli) (built: Apr 8 2016 00:08:04) ( NTS )Copyright (c) 1997-2016 The PHP GroupZend 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.confcp /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.servicePIDFile=/usr/local/php7/var/run/php-fpm.pidExecStart=/usr/local/php7/sbin/php-fpm --nodaemonize --fpm-config /usr/local/php7/etc/php-fpm.conf# 重新载入 systemdsystemctl daemon-reloadsystemctl 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 anyRTC</p>'; ?> <?php phpinfo(); ?> </body></html>
复制代码
测试是否可用
/data/app/nginx/sbin/nginx -t /data/app/nginx/sbin/nginx -s reload
复制代码
参考文档https://www.cnblogs.com/biglittleant/p/8979852.htmlFull Example Configurationhttps://zhuanlan.zhihu.com/p/97208252
划线
评论
复制
发布于: 刚刚阅读数: 3
版权声明: 本文为 InfoQ 作者【anyRTC开发者】的原创文章。
原文链接:【http://xie.infoq.cn/article/164bcee456cc8e7e774453798】。文章转载请联系作者。
anyRTC开发者
关注
实时交互,万物互联! 2020.08.10 加入
实时交互,万物互联,全球实时互动云服务商领跑者!










评论