nginx

分类:software 日期:2021-02-17 作者:admin 浏览:263

安装

编译安装

ubuntu18.04
nginx 1.18.0

  1. 下载nginx.tar.gz
  2. 安装依赖
#centos
sudo yum install gcc gcc-c++ automake pcre pcre-devel zlib zlib-devel openssl-devel -y

#ubuntu
sudo apt install build-essential libpcre3 libpcre3-dev  zlib1g zlib1g-dev libssl-dev -y

# 添加用户
useradd -s /sbin/nologin -M nginx

3.编译安装

./configure \
--user=nginx \
--group=nginx \
--prefix=/usr/local/nginx-1.18.0 \
--with-http_ssl_module \
--with-http_sub_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--with-pcre

make && make install 

配置

与php通信(主要依赖php-fpm

1.php设置

#www.conf
listen = 127.0.0.1:9000
listen = /run/www.sock

#两种其一即可但是要与nginx的fastcgi_pass配置相同

# 测试配置
/usr/local/php/sbin/php-fpm -t

2.nginx 配置

# 配置nginx
cd /usr/local/nginx
cp conf/nginx.conf conf/nginx.conf.bak

# http模块添加如下配置
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;

# 添加server模块进行测试
    server {
        listen 80;
        server_name 192.168.137.131;
        location ~ .*\.(php|php5)?$ 
        {
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_index index.php;
            include fastcgi.conf;
        }
    }

# 添加phpinfo页面
vi /usr/local/nginx/html/index.php

# 添加以下内容,通过192.168.137.131/index.php访问
<?php
phpinfo()
?>

精简配置文件

cd /usr/local/nginx/conf
cp nginx.conf nginx.conf.bak
egrep -v "#|^$" nginx.conf>nginx1.conf
mv nginx1.conf nginx.conf

拆分配置文件

  1. cd /usr/local/nginx/conf && mkdir vhost
  2. nginx.conf即主配置http模块中添加 include vhost/*.conf
  3. 在vhost文件夹中创建server的配置模块
  4. 最后重启nginx即可实现配置文件拆分

开启gzip

# 添加如下内容即可,在不同的server模块添加实现单独配置
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;
# gunzip_static on;

配置日志

# http模块去除如下注释
log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';


# server模块添加
access_log /usr/local/nginx/logs/access.log commonlog 

expires本地缓存

location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
    expires 3d;
}
# 设置本地缓存3d后过期

去除版本号

# nginx.conf > http模块
server_tokens off; 

# fastcgi.conf   
fastcgi_param  SERVER_SOFTWARE    nginx/nginx;

更多参考

评论 (暂无评论)

发表评论

昵称:  
邮箱:  
网址: