在阿里云上Ubuntu环境通过nginx+uwsgi部署Django项目

年前阿里云打折,1核1G的云服务器一年只要300多块,果断就租了1年的。既然服务器已经到手,怎么能不把自己写的项目部署上去呢,其实网上关于nginx+uwsgi部署Django项目的文章有很多,但是这些文章要不就是很久之前的,要不就是互相抄袭,一路过来都是坑,这里重点吧在部署时候遇到的坑着重介绍一下:

1. 首先部署django项目

首先是django项目,由于我是使用Anaconda来进行版本控制的,而django项目是在Python2.7环境下开发的,首先是新建一个Python2.7的环境:

#创建python27环境
conda create --name python27 python=2.7
#激活该环境
source activate python27
#通过conda安装django
conda install django

环境准备好后,从GitHub上将项目拉取下来

git clone https://github.com/sunny0826/xxx

修改setting.py文件

#debug模式默认开启,这里要关闭
DEBUG = False
#这里将外网IP配置进来
ALLOWED_HOSTS = ['127.0.0.1','xx.xx.xx.xx']

启动项目:

#来项目目录下执行
python manage.py runserver 8000

然后在外网ip:8000路径下查看项目启动情况

2. 阿里云配置安全组

由于是服务器在阿里云上,所以直接访问时访问不到的,所以要在阿里云平台配置安全组(类似于防火墙)

首先打开阿里云平台,登录,点击安全组配置:

未分类

点击配置规则

未分类

点击添加安全组规则

未分类

按要求填写安全组规则

未分类

优先级1位最高优先级,端口可以设置范围,并且可以设置授权访问的对象
设置完之后,就可以在公网IP访问到我们的django项目了

3. 安装配置uwsgi

确定django项目可以正常运行了,ctrl+c停止项目,下面我们来安装uwsgi:
在安装uwsgi非常简单,只需要pip安装即可,但是由于之前使用的是Python27环境,这里记得要激活Python27环境,否则就安装到默认的Python路径了

#uWSGI 是一个(巨大的) C 应用,所以你需要一个 C 编译器(比如 gcc 或者 clang)和 Python 开发版头文件
apt-get install build-essential python-dev
#安装uwsgi
pip install uwsgi

按照官网介绍,写已经Python脚本进行测试,test.py(在任意路径都可以,不过测完要记得删掉)

def application(env, start_response):
    start_response('200 OK', [('Content-Type','text/html')])
    return ["Hello World"]

然后输入命令进行测试

uwsgi --http :8000 --wsgi-file test.py

这里是8000端口,因为之前测试django的时候打开的8000端口,这里偷懒就不去配置新的安全组了
那么问题就来了,在启动的时候报错了….=。=

未分类

出现的这个错误,经过测试只会在Python2.7的环境发生,在Python3+就没有发生了,辗转百度和Google,都没有找到这个问题的解决办法且这个问题一般发生在nginx上…
但这个报错问题还是很明显,就是libpcre.so.1这个文件找不到,初步推测为版本问题
首先要咱们搜索一下这个文件,看看本机有没有

未分类

发现本机是有这个文件的,那就好办了,将他链接到相应位置就好

未分类

执行这条语句之后就可以uwsgi就可以正常启动了

在跳过这个坑之后,就可以正式来配置我们的项目了
首先修改自己项目中的wsgi.py文件,添加两行代码

未分类

然后在django项目根目录创建uwsgi.ini文件

uwsgi.ini文件配置

# uwsgi.ini file
[uwsgi]

# Django-related settings

socket = 127.0.0.1:8000            #与nginx通信的地址和端口

# the base directory (full path)
chdir           = /apps/xxxx        #django项目绝对路径

# Django s wsgi file
module          = xxxx.wsgi    #wsgi文件所在的相对于项目的路径

# the virtualenv (full path)
home            =  /root/anaconda3/envs/python27/lib/python2.7/site-packages        #python环境的路径

# process-related settings
# master
master          = true

# maximum number of worker processes
processes       = 4

# ... with appropriate permissions - may be needed
# chmod-socket    = 664
# clear environment on exit
vacuum          = true

plugins=python
#log文件
daemonize=/apps/uwsgi/uwsgi.log
#pid文件
pidfile=/apps/uwsgi/uwsgi.pid

这里配置日志文件和pid文件,方便之后的重启

关于uwsgi的重启问题:

每次项目有代码更新的话,都需要重启uwsgi,如果没有配置pid文件的话,重启需要使用:

#查看uwsgi进程号
ps aux | grep uwsgi
#关闭进程号为0的进程
kill -9 主进程号

未分类

来停止uwsgi,然后才能重启,注意这里的主进程是pid为1的进程,而配置了pid文件的,只需要进行如下方法来重启即可

#重载(一般修改参数,或者修改py文件经常用到)
uwsgi --reload uwsgi.pid
#重启(一般系统环境变化会用到)
uwsgi --stop uwsgi.pid

最后在项目根目录下运行uwsgi命令,载入配置文件

uwsgi --ini uwsgi.ini

然后在后面的日志中要注意环境是否为之前配置,如果不是请激活激活之前创建的Python环境再进行操作

4. 安装配置nginx

最后来安装nginx

sudo apt-get install nginx

修改nginx配置

cd /etc/nginx/sites-enabled    #Ubuntu中nginx配置文件所在路径
vim default

配置文件

server {
        listen 80 default_server;            #监听端口,将来访问的端口
        listen [::]:80 default_server;

        root /var/www/html;

        # Add index.php to the list if you are using PHP
        index index.html index.htm index.nginx-debian.html;


        server_name _;

        location / {
                include uwsgi_params;
                uwsgi_pass 127.0.0.1:8000;    #uwsgi中配置的路径
        }

        location /static {
                alias /apps/xxx/static;       #静态资源路径
        }

        location /resume.ico  {
                alias /apps/xxx/static/resume.ico;    #网页图标路径
        }
}

uwsgi启动后,再启动nginx

基础命令

/etc/init.d/nginx start  #启动
/etc/init.d/nginx stop  #关闭
/etc/init.d/nginx restart  #重启

启动之后,访问外网IP:80,查看是否可以通过nginx访问django

nginx+uwsgi在centos上部署Django应用

背景介绍:

之前做完一个Django项目,打算部署在自己的centos服务器上,可以远程访问,于是命令行上输入

python manage.py runserver 0:8000

就成功运行了,可惜,django自带的web服务器并不稳定,经常无缘无故断掉,于是想到

使用主流的uwsgi+nginx来部署项目,

至于Nginx+uWSGI+Django原理可参考http://www.cnblogs.com/Xjng/p/aa4dd23918359c6414d54e4b972e9081.html

此处就不赘述了,下面介绍下具体部署步骤

1. 安装uwsgi

pip install uwsgi

此时可以尝试使用uwsgi来启动应用,其中wsgi.py在mysite文件夹下

uwsgi --http :8000 --module mysite.wsgi

在浏览其中输入127.0.0.1:8080 能够成功访问,说明你的应用服务器部署成功,已经可以对外提供服务。

2. 安装nginx

yum install nginx

在/etc/nginx/conf.d文件夹下新建nginx启动的配置文件mysite.conf,输入以下信息

server {
  listen 8000;  #启动的nginx进程监听请求的端口
  server_name localhost;   #域名
  location / {
    include /etc/nginx/uwsgi_params;
    uwsgi_pass 127.0.0.1:8001;  #对于动态请求,转发到本机的8001端口,也就是uwsgi监听的端口
  }

  location /static/ {
    alias /home/Maximum/vacancy/static/;    #设定静态文件所在目录
  }
}

3. 同步静态文件到nginx设置的目录下

在django项目setting.py中增加

BASE_DIR = os.path.dirname(__file__)
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR,'..','vacancy','static')
然后在命令行行下执行

python manage.py collectstatic

4. 配置uwsgi

项目根目录下创建uwsgi.ini文件

vi uwsgi.ini
uwsgi.ini内容

[uwsgi]
# Django's wsgi file
module = mysite.wsgi
pythonpath = /usr/local/lib/python3.5/site-packages
socket = 127.0.0.1:8001
pidfile = /home/Maximum/uwsgi.pid    
daemonize = /home/Maximum/uwsgi.log

介绍下uwsgi和nginx相关命令

启动uwsgi:uwsgi --ini uwsgi.ini
停止uwsgi:uwsgi --stop uwsgi.pid
重新加载配置:uwsgi --reload uwsgi.pid
启动nginx:service nginx start
停止nginx:service nginx stop
重启nginx:service nginx restart

接下来启动uwsgi:

uwsgi --ini uwsgi.ini

启动nginx

service nginx start

此时没问题的话就大功告成了,访问173.199.118.8:8000/vacancies即可看到我们运行的项目了

遇到的坑

1、启动nginx时出现以下错误,可知端口已被占用

[gaarai@linode /etc/nginx]$ service nginx restart
 * Restarting nginx nginx
nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
nginx: [emerg] bind() to [::]:443 failed (98: Address already in use)
nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)

解决方法:

netstat -ntpl

查看当前运行的端口以及程序,内容类似如下:

[root@vultr mysite]# netstat -ntpl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      1502/nginx: master
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      461/sshd
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      677/master
tcp        0      0 0.0.0.0:8000            0.0.0.0:*               LISTEN      1502/nginx: master
tcp        0      0 127.0.0.1:8001          0.0.0.0:*               LISTEN      1475/uwsgi
tcp6       0      0 :::3306                 :::*                    LIS

将使用进程端口80,8000,8001的进程PID关闭,uwsgi的进程需要强制关闭

[root@vultr mysite]# kill -9 1475
[root@vultr mysite]# kill 1502

重新启动uwsgi和nginx

[root@vultr mysite]# uwsgi --ini uwsgi.ini
[root@vultr mysite]# service nginx start

2、访问网站出现Internal Server Error错误

查看uwsgi.log日志文件

tail -30 uwsgi.log

日志里出现

unable to load app 0 (mountpoint='') (callable not found or import error)

错误,

尝试执行

uwsgi --http :8000 --module mysite.wsgi

访问173.199.118.8:8000/vacancies运行正常,可知是uwsgi.ini配置文件有误

参考链接Setting up django with uwsgi and nginx

解决办法:

修改uwsgi.ini文件为

[uwsgi]
# Django's wsgi file
module = mysite.wsgi
pythonpath = /usr/local/lib/python3.5/site-packages
socket = 127.0.0.1:8001
pidfile = /home/Maximum/uwsgi.pid    
daemonize = /home/Maximum/uwsgi.log

之前有一些不太懂的设置加了上去,运行错误,去掉之后重启uwsgi和nginx服务即运行正常,

果然自己不太懂的设置还是不能乱加

以上就是

Centos 下 安装 PHP + Nginx + Sql server

一、安装前必要配置

1.1 安装epel 仓库(因为CentOs6默认的yum源没有libmcrypt-devel这个包)

yum install -y epel-release

1.2 安装必要扩展

sudo yum install libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel libcurl libcurl-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel gmp gmp-devel libmcrypt libmcrypt-devel readline readline-devel libxslt libxslt-devel gcc libmcrypt-devel

二、安装PHP

2.1 下载PHP-7.1.15

wget -c http://cn2.php.net/get/php-7.1.15.tar.gz/from/this/mirror -O php-7.1.15.tar.gz

2.2 解压 php-7.1.15.tar.gz

tar -zxvf php-7.1.15.tar.gz

2.3 进入 php-7.1.15

cd php-7.1.15

2.4 .编译与安装

sudo make && make install

这里需要一点时间

2.4 .配置php-fpm

sudo cp php.ini-production /etc/php.ini
sudo cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
sudo cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf
sudo cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
sudo chmod +x /etc/init.d/php-fpm

2.5 .启动php-fpm

···
/etc/init.d/php-fpm start

三、安装Nginx

3.1 下载Nginx

wget http://nginx.org/download/nginx-1.13.9.tar.gz

3.2 安装Nginx

tar -zxvf nginx-1.13.9.tar.gz

cd nginx-1.13.9

./configure --prefix=/usr/local/nginx

3.3 启动Nginx

sudo /usr/local/nginx/sbin/nginx 

sudo /usr/local/nginx/sbin/nginx -s reload

sudo /usr/local/nginx/sbin/nginx -s stop

四、安装FreeTDS

4.1 下载FreeTDS

wget ftp://ftp.freetds.org/pub/freetds/stable/freetds-patched.tar.gz

4.2 安装FreeTDS

需要注意的就是这里的–with-tdsver=7.1,这个非常重要

tar -zxvf freetds-0.91.tar.gz

cd freetds-0.91

./configure --prefix=/usr/local/freetds --with-tdsver=7.1 --enable-msdblib

make && make install

4.3 验证FreeTDS

/usr/local/freetds/bin/tsql -C

/usr/local/freetds/bin/tsql -H 数据库服务器IP -p 1433 -U 用户名 -P 密码

4.4 添加PHP扩展pdo_dblib

cd ../php-7.1.15/ext/pdo_dblib

/usr/local/php/bin/phpize

./configure --with-php-config=/usr/local/php/bin/php-config --with-pdo-dblib=/usr/local/freetds/

make && make install

4.5 在php.ini中增加pdo_dblib.so

extension ="pdo_dblib.so"

Linux Centos下Nginx反向代理教程

主机要求:Centos系统,内存64MB及以上、80端口没有被占用

1、升级系统、卸载Apache释放80端口

Yum update -y
Yum remove httpd -y

2、安装EPEL repo

rpm -Uvh http://mirror.ancl.hawaii.edu/linux/epel/6/i386/epel-release-6-8.noarch.rpm

EPEL repo下载地址:https://fedoraproject.org/wiki/EPEL

3、安装Nginx,并设置

安装Nginx

yum install nginx -y

调整Nginx配置

cd /etc/nginx/conf.d
mv default.conf default.conf.disabled

4、创建Nginx反代配置文件

cd /etc/nginx/conf.d
vi yourdomain.com                 

粘贴以下内容:

server {
      listen 80;
      server_name yourdomain.com;   
      access_log off;
      error_log off;
      location / {
      proxy_pass http://需要反代的服务器IP/;
      proxy_redirect off;
      proxy_set_header Host $host;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_max_temp_file_size 0;
      client_max_body_size 10m;
      client_body_buffer_size 128k;
      proxy_connect_timeout 90;
      proxy_send_timeout 90;
      proxy_read_timeout 90;
      proxy_buffer_size 4k;
      proxy_buffers 4 32k;
      proxy_busy_buffers_size 64k;
      proxy_temp_file_write_size 64k;
   }
} 

然后保存。

5、设置防火墙,允许80端口访问

iptables -I INPUT 5 -m state --state NEW -p tcp --dport 80 -j ACCEPT 
service iptables save 
service iptables restart

6、启动Nginx

service nginx start

nginx代理tomcat导致css或js加载失败

Web服务器经常会使用nginx作前置路由,在使用nginx配置Web服务器负载均衡、动静分离,会碰到设置反向代理后导致前端资源无法加载的问题

nginx反向代理的tomcat服务器导致前端资源css或js加载失败大概可以分为:端口丢失、真实ip或端口获取错误、js或者css太大加载失败等情形

端口丢失

之前笔者也有文章单独介绍,可参考http://flyflyfish.com/2018/02/24/nginx%E8%BD%AC%E5%8F%91%E4%B8%A2%E5%A4%B1%E7%AB%AF%E5%8F%A3%E9%97%AE%E9%A2%98/

反向代理获取真实ip(域名)、端口、协议

nginx反向代理后,servlet应用通过request.getRemoteAddr()取到的IP是nginx的IP,并非客户端的真实IP;通过request.getRequestUrl()获取的ip(域名)、端口、协议都是nginx对应的参数。

  • 比如nginx的配置:
http {
    upstream backend {
        server 127.0.0.1:8080;
        #server backend1.example.com wight=5;
        #server 127.0.0.1:8080 max_fails=3 fail_timeout=30s;

        #server backup1.example.com backup;
    }

    server {
        listen 80;
        server_name your.domain.com;

        location /test {
            proxy_pass http://backend/test;
        }
        ...
    }
}

在浏览器打开http://your.domain.com/test访问servlet应用,获取客户端IP和URL:

log.info("RemoteAddr:{}, URL:{}", request.getRemoteAddr(), request.getRequestURL());
//输出结果
RemoteAddr:127.0.0.1, URL:http://127.0.0.1:8080/test
  • 针对tomcat+nginx的解决方案:

nginx添加如下配置,:

proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;

如果不知道如何添加,可参考nginx转发丢失端口的问题。添加完后,输出结果如下:

//输出结果
RemoteAddr:127.0.0.1, URL:http://浏览器的ip地址/test

发现RemoteAddr仍然获取不正确,解决方案如下:

  • 方案一:通过request.getHeader("X-Forwrad-For")request.getHeader("X-Real-IP")获取到nginx配置的Header。

  • 方案二: 配置tomcat,通过Servlet APIrequest.getRemoteAddr()方法获取客户端的IP。Tomcat的server.xml,在Host元素内最后加入:

<Valve className="org.apache.catalina.valves.RemoteIpValve" />

JS或css无法完全加载

nginx的代理缓存区,默认较小导致部分文件出现加载不全的问题,比较典型的如jQuery框架,可以通过配置调整nginx的缓存区即可。

最终完整配置如下:

http {
    # http_proxy 
    proxy_buffer_size 128k;
    proxy_buffers 32 128k;
    proxy_busy_buffers_size 128k;

    upstream backend {
        server 127.0.0.1:8080;
    }

    server {
        listen 80;
        server_name your.domain.com;

        location /test {
            proxy_pass http://backend/test;
            # proxy_params
            proxy_set_header Host $http_host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
        }
        ...
    }
    ...
}

关于nginx的http_proxy模块参数含义:

未分类

参考链接:

  • https://lanjingling.github.io/2015/10/28/nginx-tomcat-cluster/

  • http://www.zving.com/c/2014-03-19/257610.shtml

  • http://blog.csdn.net/xiao__gui/article/details/73733797

  • https://www.jianshu.com/p/5c7d718f15d2

利用nginx的fastcgi_cache缓存加速WordPress

WordPress有很多的缓存加速方案,例如插件缓存(wp-super-cache、wp-rocket等)、PHP代码缓存等等,现分享本站使用的nginx缓存。利用fastcgi_cache缓存。

未分类

在使用nginx缓存之前,必须在nginx里面加载专门的模块,这个模块叫做ngx_cache_purge。

添加ngx_cache_purge模块

下载ngx_cache_purge模块

ngx_cache_purge模块的官方地址:http://labs.frickle.com/files/。在这个地址找到最新版的模块版本 ,使用wget下载。

wget http://labs.frickle.com/files/ngx_cache_purge-2.3.tar.gz
tar zxvf ngx_cache_purge-2.3.tar.gz

我这里使用的就是ngx_cache_purge-2.3。

编译安装ngx_cache_purge模块

使用nginx -V命令查看nginx是否已经安装了这个模块,如果没有安装,需要重新编译安装。

使用军哥lnmp一键安装包的同学,可以在lnmp的安装目录中找到lnmp.conf这个文件,然后在nginx模块中添加ngx_cache_purge。之后重新平滑升级nginx即可。

修改ngxin配置

在使用fastcgi_cache缓存之前,必须先修改nginx配置,具体就是进入虚拟主机配置中,找到domainname.conf,然后修改里面的sever配置。

#下面2行的中的wpcache路径请自行提前创建,否则可能会路径不存在而无法启动nginx,max_size请根据分区大小自行设置
fastcgi_cache_path /tmp/wpcache levels=1:2 keys_zone=WORDPRESS:250m inactive=1d max_size=1G;
fastcgi_temp_path /tmp/wpcache/temp;
fastcgi_cache_key "$scheme$request_method$host$request_uri";
fastcgi_cache_use_stale error timeout invalid_header http_500;
#忽略一切nocache申明,避免不缓存伪静态等
fastcgi_ignore_headers Cache-Control Expires Set-Cookie;
#Ps:如果是多个站点,以上内容不要重复添加,否则会冲突,可以考虑将以上内容添加到nginx.conf里面,避免加了多次。
server
    {
        listen 80;
        #请修改为自己的域名
        server_name zhangge.net;
        index index.html index.htm index.php default.html default.htm default.php;
        #请修改为自己网站的存放路径
        root  /home/wwwroot/domainname.com;
        set $skip_cache 0;
        #post访问不缓存
        if ($request_method = POST) {
            set $skip_cache 1;
        }
        #动态查询不缓存
        if ($query_string != "") {
            set $skip_cache 1;
        }
        #后台等特定页面不缓存(其他需求请自行添加即可)
        if ($request_uri ~* "/wp-admin/|/xmlrpc.php|wp-.*.php|/feed/|index.php|sitemap(_index)?.xml") {
            set $skip_cache 1;
        }
        #对登录用户、评论过的用户不展示缓存(这个规则张戈博客并没有使用,所有人看到的都是缓存)
        if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in") {
            set $skip_cache 1;
        }
        #这里请参考你网站之前的配置,特别是sock的路径,弄错了就502了!
        location ~ [^/].php(/|$)
            {
                try_files $uri =404;
                fastcgi_pass  unix:/tmp/php-cgi.sock;
                fastcgi_index index.php;
                include fastcgi.conf;
                #新增的缓存规则
                fastcgi_cache_bypass $skip_cache;
                fastcgi_no_cache $skip_cache;
                add_header X-Cache "$upstream_cache_status From $host";
                fastcgi_cache WORDPRESS;
                fastcgi_cache_valid 200 301 302 1d;
        }
        location / {
                #此处可以添加自定义的伪静态规则(之前你新增的伪静态规则可以添加到这,没有就不用了)
                try_files $uri $uri/ /index.php?$args;
                rewrite /wp-admin$ $scheme://$host$uri/ permanent;
         }
        #缓存清理配置(可选模块,请细看下文说明)
        location ~ /purge(/.*) {
            allow 127.0.0.1;
            allow "此处填写你服务器的真实外网IP";
            deny all;
            fastcgi_cache_purge WORDPRESS "$scheme$request_method$host$1";
        }
        location ~* ^.+.(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|rss|atom|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$ {
                access_log off; log_not_found off; expires max;
        }
        location = /robots.txt { access_log off; log_not_found off; }
        location ~ /. { deny  all; access_log off; log_not_found off; }
        #请注意修改日志路径
        access_log /home/wwwlogs/domainname.com.log access;

注意修改上述代码中的该修改部分,不然nginx重启会出错。当然,如果是启用了https,模块就应相应的改变。

安装Nginx-helper插件

在后台搜索nginx-helper,安装好插件。

关于插件的设置:

如果没有使用CDN就可以选择purge模式,如果使用了CDN最好选择文件模式。

由于插件作者定义的缓存路径是 /var/run/nginx-cache ,而我们可能会根据服务器实际情况来自定义缓存路径,这样一来,缓存路径的不同就会导致插件无法找到缓存文件并删除!

解决的方法:在wp-config.php中增加一行代码:

define( 'RT_WP_NGINX_HELPER_CACHE_PATH','/tmp/wpcache');

这样,就配置好了。

最新nginx内置变量

在配置基于nginx服务器的网站时,必然会用到 nginx内置变量 ,下面笔者将它整理成列表,把最新版本的变量列出来,以方便做配置时查询

nginx内置变量

内置变量存放在 ngx_http_core_module 模块中,变量的命名方式和apache 服务器变量是一致的。总而言之,这些变量代表着客户端请求头的内容,例如$http_user_agent, $http_cookie, 等等。下面是nginx支持的所有内置变量:

$arg_name

请求中的的参数名,即“?”后面的arg_name=arg_value形式的arg_name

$args

请求中的参数值

$binary_remote_addr

客户端地址的二进制形式, 固定长度为4个字节

$body_bytes_sent

传输给客户端的字节数,响应头不计算在内;这个变量和Apache的mod_log_config模块中的“%B”参数保持兼容

$bytes_sent

传输给客户端的字节数 (1.3.8, 1.2.5)

$connection

TCP连接的序列号 (1.3.8, 1.2.5)

$connection_requests

TCP连接当前的请求数量 (1.3.8, 1.2.5)

$content_length

“Content-Length” 请求头字段

$content_type

“Content-Type” 请求头字段

$cookie_name

cookie名称

$document_root

当前请求的文档根目录或别名

$document_uri

同 $uri

$host

优先级如下:HTTP请求行的主机名>”HOST”请求头字段>符合请求的服务器名

$hostname

主机名

$http_name

匹配任意请求头字段; 变量名中的后半部分“name”可以替换成任意请求头字段,如在配置文件中需要获取http请求头:“Accept-Language”,那么将“-”替换为下划线,大写字母替换为小写,形如:$http_accept_language即可。

$https

如果开启了SSL安全模式,值为“on”,否则为空字符串。

$is_args

如果请求中有参数,值为“?”,否则为空字符串。

$limit_rate

用于设置响应的速度限制,详见 limit_rate。

$msec

当前的Unix时间戳 (1.3.9, 1.2.6)

$nginx_version

nginx版本

$pid

工作进程的PID

$pipe

如果请求来自管道通信,值为“p”,否则为“.” (1.3.12, 1.2.7)

$proxy_protocol_addr

获取代理访问服务器的客户端地址,如果是直接访问,该值为空字符串。(1.5.12)

$query_string

同 $args

$realpath_root

当前请求的文档根目录或别名的真实路径,会将所有符号连接转换为真实路径。

$remote_addr

客户端地址

$remote_port

客户端端口

$remote_user

用于HTTP基础认证服务的用户名

$request

代表客户端的请求地址

$request_body

客户端的请求主体

此变量可在location中使用,将请求主体通过proxy_pass, fastcgi_pass, uwsgi_pass, 和 scgi_pass传递给下一级的代理服务器。

$request_body_file

将客户端请求主体保存在临时文件中。文件处理结束后,此文件需删除。如果需要之一开启此功能,需要设置client_body_in_file_only。如果将次文件传递给后端的代理服务器,需要禁用request body,即设置proxy_pass_request_body off,fastcgi_pass_request_body off, uwsgi_pass_request_body off, or scgi_pass_request_body off 。

$request_completion

如果请求成功,值为”OK”,如果请求未完成或者请求不是一个范围请求的最后一部分,则为空。

$request_filename

当前连接请求的文件路径,由root或alias指令与URI请求生成。

$request_length

请求的长度 (包括请求的地址, http请求头和请求主体) (1.3.12, 1.2.7)

$request_method

HTTP请求方法,通常为“GET”或“POST”

$request_time

处理客户端请求使用的时间 (1.3.9, 1.2.6); 从读取客户端的第一个字节开始计时。

$request_uri

这个变量等于包含一些客户端请求参数的原始URI,它无法修改,请查看$uri更改或重写URI,不包含主机名,例如:”/cnphp/test.php?arg=freemouse”。

$scheme

请求使用的Web协议, “http” 或 “https”

$sent_http_name

可以设置任意http响应头字段; 变量名中的后半部分“name”可以替换成任意响应头字段,如需要设置响应头Content-length,那么将“-”替换为下划线,大写字母替换为小写,形如:$sent_http_content_length 4096即可。

$server_addr

服务器端地址,需要注意的是:为了避免访问linux系统内核,应将ip地址提前设置在配置文件中。

$server_name

服务器名,www.cnphp.info

$server_port

服务器端口

$server_protocol

服务器的HTTP版本, 通常为 “HTTP/1.0” 或 “HTTP/1.1”

$status

HTTP响应代码 (1.3.2, 1.2.2)

$tcpinfo_rtt, $tcpinfo_rttvar, $tcpinfo_snd_cwnd, $tcpinfo_rcv_space

客户端TCP连接的具体信息

$time_iso8601

服务器时间的ISO 8610格式 (1.3.12, 1.2.7)

$time_local

服务器时间(LOG Format 格式) (1.3.12, 1.2.7)

$uri

请求中的当前URI(不带请求参数,参数位于$args),可以不同于浏览器传递的$request_uri的值,它可以通过内部重定向,或者使用index指令进行修改,$uri不包含主机名,如”/foo/bar.html”。

Rails Nginx 实现下载受限文件的优化

需求

在Web开发中经常遇到一些文件需要用户认证和授权才能够访问,实现这样的功能需要在App Server进行。当遇到大文件或者并发量增加的时候,容易造成性能问题。为什么会这样呢?让我们从操作系统层面来分析一下:

未分类

当下载一个文件的时候,应用程序会进行系统调用。操作系统首先会检查,是不是最近访问过此文件,文件内容是否缓存在内核缓冲区,如果是,操作系统则直接根据read系统调用提供的buf地址,将内核缓冲区的内容拷贝到buf所指定的用户空间缓冲区中去。如果不是,操作系统则首先将磁盘上的数据拷贝的内核缓冲区,这一步目前主要依靠DMA来传输,然后再把内核缓冲区上的内容拷贝到用户缓冲区中。接下来,write系统调用再把用户缓冲区的内容拷贝到网络堆栈相关的内核缓冲区中,最后socket再把内核缓冲区的内容发送到网卡上。

综上一共进行了4次数据的拷贝和4次上下文切换。那么有没有方法减少数据的拷贝和上下文切换?从Linux Kernel 2.4版本开始新增了sendfile()来实现这些过程的简化。

#include<sys/sendfile.h>
ssize_t sendfile(int out_fd, int in_fd, off_t *offset, size_t count);

未分类

系统调用sendfile()在代表输入文件的描述符in_fd和代表输出文件的描述符out_fd之间传送文件内容(字节)。描述符out_fd必须指向一个套接字,而in_fd指向的文件必须是可以mmap的。这些局限限制了sendfile的使用,使sendfile只能将数据从文件传递到套接字上,反之则不行。

使用sendfile不仅减少了数据拷贝的次数,还减少了上下文切换,数据传送始终只发生在kernel space。

那么我们该如何在实际应用中使用sendfile()呢?幸好Rails和Nginx给我们提供高级的抽象,我们只需要简单的配置一下就可食用。

实现和配置

假设

有一个请求访问受限文件test.txt, 该文件放在/mnt/data/test.txt

GET /api/download/files/test.txt HTTP/1.1

Nginx 配置

location /_send_file_accel/{
    internal;
    alias /mnt/data/;
}

location /api {
    ...

    proxy_set_header   X-Sendfile-Type X-Accel-Redirect;
    proxy_set_header   X-Accel-Mapping /mnt/data/=/_send_file_accel/;

    ...
}

Rails 配置

只需要在production.rb的config文件中加入

config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect'

Rails controller 中的代码

def show
  ...
  file = user.test_file # 假设文件名为test.txt
  path = File.join('/mnt/data', file)
  send_file(path, 
    disposition: 'attachment', 
    status: 200)
end

切割Nginx日志文件

以前用Apache的时候有自带个叫rotatelogs的工具,只需要修改httpd.conf文件就可以轻松简单分割日志文件。
不过Nginx并没有自带这样的工具,还需要写脚本用crontab执行。

#!/bin/bash
#设置日志文件目录
logs_path="/home/wwwlogs/"
#设置pid文件
pid_path="/usr/local/nginx/logs/nginx.pid"

#重命名日志文件
mv ${logs_path}access.log ${logs_path}access_$(date -d "yesterday" +"%Y%m%d").log

#向nginx主进程发信号重新打开日志
kill -USR1 `cat ${pid_path}`

其中日志文件目录和pid目录请根据自已的情况更改,pid文件存放的目录打开nginx.conf就可以看到了。

平常kill命令经常拿来杀进程,在这里用了个-USR1参数,在网上查了一下,有这样一段说明:

USR1亦通常被用来告知应用程序重载配置文件;例如,向HTTP服务器发送一个USR1信号将导致以下步骤的发生:停止接受新的连接,等待当前连接停止,重新载入配置文件,重新打开日志文件,重启服务器,从而实现相对平滑的不关机的更改。

真素打开了新世界的大门

最后把上面那段脚本保存,比如我们存在/usr/local/nginx/rotatelogs.sh
然后设置每天凌晨12点执行这段脚本:

0 0 * * * bash /usr/local/nginx/rotatelogs.sh

如果你有多个网站,多个日志的话,在重命名日志文件那里多加相应的几个进去就可以了。
像博主这样VPS没有默认安装crontab的,可以yum install crontabs。
然后再使用crontab -e命令进行脚本的添加。
一般来说都会打开一个vi编辑器,具体就请参照vi编辑器的用法啦。

补充:

在crontab中%是有特殊含义的,表示换行的意思。如果要用的话必须进行转义%,如经常用的date ‘+%Y%m%d’在crontab里是不会执行的,应该换成date ‘+%Y%m%d’。
我们的脚本里也有类似的表示日期的,不过不是直接写在crontab里就不用考虑转义了。

参考:

nginx日志切割

NGINX 已支持 SERVER PUSH

2018-02-20 刚刚更新的 nginx-1.13.9 已支持 HTTP/2 的特性 Server Push ,这个特性目的是让服务端将部分资源主动推送给浏览器,节约浏览器需要使用这些资源时再次发送 Get 请求的时间。很长的一段时间内 Nginx 都是不支持这个特性的,不过在新版本中已经可以使用,详细改动可以查看 http://hg.nginx.org/nginx/rev/641306096f5b

Nginx 中的 Server Push 提供两个选项:

直接指定需要推送的资源,Nginx 会直接推送(只能使用相对链接)。

http2_push  /css/style.css;

使用 W3C文档 规定的 Link 字段作为 HTML 响应。

Link: </style.css>; as=style; rel=preload;

然后在配置中开启

http2_push_preload on;

为了方便,我选择第一种方式,直接指定需要推送的资源文件。之后在 Chrome 的 Developer Tools —— Network 面板中可以看到我指定推送的资源状态为 Push 。