suse 上apache 启用 module_headers 并配置 cache-control

启用 headers_moule

在 centos 配置比较简单,只需要 loadmodules 即可:

cat /etc/httpd/conf.modules.d/00-base.conf | grep headers
LoadModule headers_module modules/mod_headers.so

但在 suse 上,却是通过 sysconfig 来配置的:


cat /etc/sysconfig/apache2 | grep MODULES | grep -v "^#"
APACHE_MODULES="actions alias auth_basic authn_core authn_file authz_core authz_groupfile authz_host authz_user autoindex cgi dir env expires include log_config mime negotiation perl reqtimeout setenvif socache_shmcb ssl userdir headers"

需要编辑这行内容,并且如果 /usr/lib64/apache2 下有 so 文件即可。查看模块方式如下:

/usr/sbin/apache2ctl -t -D DUMP_MODULES | grep shared
AH00557: httpd-prefork: apr_sockaddr_info_get() failed for mirrors
AH00558: httpd-prefork: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1. Set the 'ServerName' directive globally to suppress this message
actions_module (shared)
alias_module (shared)
auth_basic_module (shared)
authn_core_module (shared)
authn_file_module (shared)
authz_core_module (shared)
authz_groupfile_module (shared)
authz_host_module (shared)
authz_user_module (shared)
autoindex_module (shared)
cgi_module (shared)
dir_module (shared)
env_module (shared)
expires_module (shared)
include_module (shared)
log_config_module (shared)
mime_module (shared)
negotiation_module (shared)
perl_module (shared)
reqtimeout_module (shared)
setenvif_module (shared)
socache_shmcb_module (shared)
ssl_module (shared)
userdir_module (shared)
headers_module (shared)

其中静态和动态加载的都显示出来了。

## 配置header中的cache-control

比如其中的一个配置为:


PerlRequire "/etc/apache2/smt-mod_perl-startup.pl"

Alias "/SUSE" "/srv/www/htdocs/repo/SUSE"
Alias repo "/srv/www/htdocs/repo"

<Directory "/srv/www/htdocs/repo">
    Options +Indexes +FollowSymLinks
    IndexOptions +NameWidth=*
    PerlAuthenHandler NU::SMTAuth
    AuthName SMTAuth
    AuthType Basic
    Require valid-user

    <IfModule mod_headers.c>
            Header set Via "host-name"
            Header set Cache-Control "max-age=43200"
            <FilesMatch "\.(xml|xm_|gz|sh|conf|tar|repo|bz2)$">
                    Header set Cache-Control "max-age=300"
            </FilesMatch>
    </IfModule>

    # Allow unauthenticated access to /repo/tools/ directory
    Require expr %{REQUEST_URI} =~ m#^/repo/tools/.*#
</Directory>



设置了两个 header, 并且根据文件后缀来设置不同的值。

验证效果

xml 文件


curl -I http://localhost/repo/SUSE/Updates/SLE-SAP/12-SP2/x86_64/update/repodata/repomd.xml
HTTP/1.1 200 OK
Date: Thu, 27 Sep 2018 03:41:23 GMT
Server: Apache
Last-Modified: Tue, 25 Sep 2018 16:06:01 GMT
ETag: "c22-576b448324c40"
Accept-Ranges: bytes
Content-Length: 3106
Via: smt-root-b
Cache-Control: max-age=300
Content-Type: text/xml

其他文件


curl -I http://localhost/repo/SUSE/Updates/SLE-SAP/12-SP2/x86_64/update/x86_64/MozillaFirefox-52.8.0esr-109.31.2.x86_64.rpm
HTTP/1.1 200 OK
Date: Thu, 27 Sep 2018 03:41:01 GMT
Server: Apache
Last-Modified: Fri, 18 May 2018 06:05:07 GMT
ETag: "2bfe4cd-56c74bbd72ac0"
Accept-Ranges: bytes
Content-Length: 46130381
Via: smt-root-b
Cache-Control: max-age=43200
Content-Type: application/x-rpm

Posted in web和容器 | suse 上apache 启用 module_headers 并配置 cache-control已关闭评论

nginx + uwsgi 配置 flask 开发框架

介绍

类似于,nginx 结合 php-fpm 可以支持跳转到 php 程序一样,nginx 后端也可以挂 uwsgi 来用 python 程序作为server端。

参考文档

《一个UWSGI的例子》:https://blog.csdn.net/crazyhacking/article/details/18617873
主要是关于flask + python 的代码开发例子。
《uWSGI+Nginx+Flask在Linux下的部署》https://www.cnblogs.com/zhangjpn/p/6876412.html?utm_source=itdadao&utm_medium=referral
主要是关于部署

软件安装

pip install uwsgi
yum install python-flask
yum install nginx

配置

nginx 配置

使用 http/http-socket 协议

server {
            listen       80 default_server;
            server_name  _;

        location ~ /pptserver { 
            proxy_pass http://127.0.0.1:8888;
            }

    error_page 404 /404.html;
        location = /40x.html {
    }

    error_page 500 502 503 504 /50x.html;
        location = /50x.html {
    }

}

使用unix socket 协议(本地文件)

server {
            listen       80 default_server;
            server_name  _;

        location ~ /pptserver { 
        include uwsgi_params;
        uwsgi_pass unix://tmp/ppt.sock;
            }

    error_page 404 /404.html;
        location = /40x.html {
    }

    error_page 500 502 503 504 /50x.html;
        location = /50x.html {
    }

}

注意,要确保 sock 文件可以被访问,一般设置为 777 即可。

uwsgi 配置

cat /data/install/conf/uwsgi.conf 
[uwsgi]
## For directlly http access
http-socket=127.0.0.1:8888

### For nginx proxy
#socket=/tmp/ppt.sock

wsgi-file=/data/install/bin/pptserver.py
#plugins = python
callable = app
#chdir = /data/install/bin
touch-reload=/data/install/bin/
processes = 2
threads = 2
stats = 127.0.0.1:9191
post-buffering = 8192
buffer-size = 65535
socket-timeout = 10
uid = apache
gid = apache
master = true
#protocol = uwsgi

注意:使用 http或者socket协议时,只需要切换下(socket或者http)即可。

运行程序

启动 nginx

service nginx restart

启动 uwsgi

/usr/bin/uwsgi --ini /data/install/conf/uwsgi.conf

测试

# curl "localhost/pptserver?uid=3434&name=get_jingle" -d "name=post_jigang"
{"get_name":"get_jingle","post_name":"post_jigang"}

附件

pptserver.py

# cat /data/install/bin/pptserver.py 
from flask import Flask,render_template, jsonify
from flask import request

app = Flask(__name__)

@app.route('/pptserver', methods=['GET', 'POST'])
#def index():
def index():
    requester = request.remote_addr
        #logging.info('Data request from: %s' % requester)
        url = request.url
        uid = url.split('uid=')[-1]
    get_name=request.args.get("name")
    post_name=request.form.get("name")
    #return "Test message, your input uid=%s, method=%s, get_name=%s, post_name=%s  from %s\n" % (uid, request.method, get_name, post_name, requester)
    return jsonify(get_name=get_name,post_name=post_name)
if __name__ == '__main__':
    app.run()

git 工程

参考我的 git 示例工程

https://github.com/duanjigang1983/study

Posted in web和容器 | nginx + uwsgi 配置 flask 开发框架已关闭评论

nginx + php-fpm 的简易配置

nginx 转发到 php-fpm

安装软件

yum install nginx -y
yum install php-fpm -y

配置nginx

在 /etc/nginx/conf.d/default.conf 基础上直接修改,就可以作为你的应用使用:

server {
    listen       80 default_server;
    server_name  _;
    root         /usr/share/nginx/html;
    include /etc/nginx/default.d/*.conf;

    location / {
    }

        #location ~ .php$ { ## 针对所有php跳转
        location ~ /testapi { ## 针对testapi底下的文件跳转。
            #root /var/www/html/opsxapi;
            root /var/www/html;
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_index index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
            }

    error_page 404 /404.html;
        location = /40x.html {
    }

    error_page 500 502 503 504 /50x.html;
        location = /50x.html {
    }

}

比如我们在 /var/www/html 下部署了 testapi 这个目录,其中放置了 test.php
当启动 nginx 和 php-fpm 后,可以测试

curl http://127.0.0.1/testapi/test.php

就能够请求到这页面了。
其实的原理就是,匹配到 “/testapixxxx”这个规则后,然后返回:
/var/www/html + /testapixxxx 执行的结果。

Posted in DEVOPS, web和容器 | nginx + php-fpm 的简易配置已关闭评论