OpenResty 安装配置


0. 说明


 1. Windows 下安装

  下载软件包 openresty-1.13.6.1-win32.zip ,解压即可食用。

  【开启】

  直接运行 nginx.exe

  在 Windows 的命令窗口执行 netstat -ano 验证是否开启成功

  

 


2. Linux 下安装

  2.0 说明

  采用 yum 的方式将 OpenResty 安装在 CentOS 7 中

  2.1 添加 yum 源

# 在 /etc/yum.repos.d 目录下创建 openresty.repo 文件
echo > openresty.repo

# 在 openresty.repo 中添加内容如下

[openresty]
name=Official OpenResty Open Source Repository for CentOS
baseurl=https://openresty.org/package/centos/$releasever/$basearch
skip_if_unavailable=False
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://openresty.org/package/pubkey.gpg
enabled=1
enabled_metadata=1

  2.2 清空 yum 缓存并重建缓存

# 切换到root账户下操作
su root
yum clean all
yum makecache

  2.3 通过 yum 进行安装

# 搜索 openresty 软件包
sudo yum cache search openresty

# 安装 openresty
sudo yum install openresty

  2.4 确定安装的位置

[centos@s101 /usr/local]$ which openresty
/usr/bin/openresty

  2.5 启动 OpenResty

# 查看进程是否启动
ps -Af | grep nginx

# 启动进程
openresty

# 查看进程是否启动
ps -Af | grep nginx

  


3. OpenResty 管理命令

   3.1 启动

# 切换到 root 账户
su root 

# 启动命令
openresty 

  3.2 停止

# 停止命令
openresty -s stop

  3.3 重新加载配置文件

# 重新加载
openresty -s reload

  3.4 检查配置文件正确性

# 检查文件
openresty -t

  3.5 查看帮助

# 查看帮助
sudo openresty -h

4. 配置 CentOS 上 Nginx Web 服务器

  4.1 修改 nginx.conf 

  在 /usr/local/openresty/nginx/conf 目录下修改 nginx.conf

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    log_format main escape=json $msec#$time_local#$clientRealIp#$http_client_time#$status#$request_body;
    #access_log  logs/access.log  main;
    access_log logs/access.log main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;
    
    map $http_x_forwarded_for $clientRealIp {
        ~^(?P<firstAddr>[0-9.]+),?.*$ $firstAddr;
    }

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
            error_page 405 =200 $1;
            lua_need_request_body on;
            content_by_lua 'local s =ngx.var.request_body';
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ .php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ .php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

   4.2 在 /usr/local/openresty/nginx/html 下新建 1.html

<!DOCTYPE html>
<html>
<head>
<title>1.html</title>
</head>
<body>
        s101
</body>
</html>

  4.3 验证

# 在关闭防火墙的情况下在浏览器中输入以下
http://s101/1.html

  4.4 关闭 crond 服务

[root@s101 /usr/local/openresty/nginx/html]# service crond stop
Redirecting to /bin/systemctl stop  crond.service

5. Windows下配置反向代理服务器 

  修改配置文件 openresty-1.13.6.2-win64conf ginx.conf

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';
    
    log_format main escape=json $remote_addr#$http_client_time#$time_local#$status#$request_body;

    #access_log  logs/access.log  main;
    access_log logs/access.log main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;
    
    #反向代理
    upstream servers{
        server s101:80 weight=1;
        server s102:80 weight=1;
    }
    map $http_x_forwarded_for  $clientRealIp {
        ~^(?P<firstAddr>[0-9.]+),?.*$    $firstAddr;
    }


    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;
        
        underscores_in_headers on;

        location / {
            #root   html;
            #index  index.html index.htm;
            error_page 405 =200 $1;
            lua_need_request_body on;
            content_by_lua 'local s = ngx.var.request_body';
            proxy_pass http://servers;
            proxy_set_header Host $host;
            proxy_set_header remote_user_ip $remote_addr;
            proxy_set_header X-Forwarded-For
            $proxy_add_x_forwarded_for;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ .php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ .php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

6. 测试代码 

import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * 入口点程序
 */
public class Main {
    public static void main(String[] args) throws Exception {
        String strURL = "http://localhost/1.html" ;
        URL url = new URL(strURL) ;
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        //设置请求方式
        conn.setRequestMethod("POST");
        //设置发送的内容类型
        conn.setRequestProperty("Content-Type" , "application/json");
        //允许输出到服务器
        conn.setDoOutput(true);
        OutputStream out = conn.getOutputStream() ;

        String json = "{
" + "  "appChannel": "anroid bus",
" + "  "appId": "sohuvideo",
" + "  "appPlatform": "ios",
" + "  "appVersion": "1.1.0",
" + "  "deviceStyle": "oppo 1",
" + "  "errorLogs": [
" + "    {
" + "      "appChannel": "umeng",
" + "      "appId": "gaodemap",
" + "      "appPlatform": "blackberry",
" + "      "appVersion": "1.1.0",
" + "      "deviceStyle": "vivo 3",
" + "      "errorBrief": "at cn.lift.dfdf.web.AbstractBaseController.validInbound(AbstractBaseController.java:72)",
" + "      "errorDetail": "at cn.lift.dfdfdf.control.CommandUtil.getInfo(CommandUtil.java:67) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606)",
" + "      "osType": "android 4.0",
" + "      "tenantId": "tnt023"
" + "    }
" + "  ],
" + "  "eventLogs": [
" + "    {
" + "      "appChannel": "anroid bus",
" + "      "appId": "tianya",
" + "      "appPlatform": "android",
" + "      "appVersion": "2.0.0",
" + "      "deviceStyle": "红米",
" + "      "eventId": "popmenu",
" + "      "osType": "ios11",
" + "      "tenantId": "tnt009"
" + "    },
" + "    {
" + "      "appChannel": "appstore",
" + "      "appId": "gaodemap",
" + "      "appPlatform": "android",
" + "      "appVersion": "1.1.0",
" + "      "deviceStyle": "iphone 7 plus",
" + "      "eventId": "popmenu",
" + "      "osType": "android 4.0",
" + "      "tenantId": "tnt009"
" + "    },
" + "    {
" + "      "appChannel": "appstore",
" + "      "appId": "faobengplay",
" + "      "appPlatform": "blackberry",
" + "      "appVersion": "1.0.0",
" + "      "deviceStyle": "vivo 3",
" + "      "eventId": "autoImport",
" + "      "osType": "ios11",
" + "      "tenantId": "tnt009"
" + "    },
" + "    {
" + "      "appChannel": "anroid bus",
" + "      "appId": "tianya",
" + "      "appPlatform": "blackberry",
" + "      "appVersion": "1.1.0",
" + "      "deviceStyle": "iphone 7 plus",
" + "      "eventId": "popmenu",
" + "      "osType": "mi 5.5",
" + "      "tenantId": "tnt009"
" + "    },
" + "    {
" + "      "appChannel": "anroid bus",
" + "      "appId": "tianya",
" + "      "appPlatform": "android",
" + "      "appVersion": "1.2.0",
" + "      "deviceStyle": "iphone 7",
" + "      "eventId": "bookstore",
" + "      "osType": "ios11",
" + "      "tenantId": "tnt501"
" + "    }
" + "  ],
" + "  "osType": "ios11",
" + "  "pageLogs": [
" + "    null,
" + "    null,
" + "    null,
" + "    null,
" + "    null
" + "  ],
" + "  "startupLogs": [
" + "    {
" + "      "appChannel": "anroid bus",
" + "      "appId": "faobengplay",
" + "      "appPlatform": "ios",
" + "      "appVersion": "1.2.0",
" + "      "brand": "魅族",
" + "      "carrier": "中国铁通",
" + "      "country": "america",
" + "      "deviceStyle": "vivo 3",
" + "      "network": "wifi",
" + "      "osType": "android 4.0",
" + "      "province": "guangdong",
" + "      "screenSize": "480 * 320",
" + "      "tenantId": "tnt501"
" + "    },
" + "    {
" + "      "appChannel": "appstore",
" + "      "appId": "sohuvideo",
" + "      "appPlatform": "blackberry",
" + "      "appVersion": "2.0.0",
" + "      "brand": "Apple",
" + "      "carrier": "中国铁通",
" + "      "country": "china",
" + "      "deviceStyle": "iphone 7",
" + "      "network": "3g",
" + "      "osType": "ios11",
" + "      "province": "guangxi",
" + "      "screenSize": "1136 * 640",
" + "      "tenantId": "tnt501"
" + "    }
" + "  ],
" + "  "tenantId": "tnt009",
" + "  "usageLogs": [
" + "    {
" + "      "appChannel": "umeng",
" + "      "appId": "gaodemap",
" + "      "appPlatform": "winphone",
" + "      "appVersion": "1.1.0",
" + "      "deviceStyle": "iphone 7",
" + "      "osType": "ios11",
" + "      "tenantId": "tnt009"
" + "    },
" + "    {
" + "      "appChannel": "anroid bus",
" + "      "appId": "tianya",
" + "      "appPlatform": "android",
" + "      "appVersion": "1.2.0",
" + "      "deviceStyle": "iphone 6",
" + "      "osType": "android 4.0",
" + "      "tenantId": "tnt501"
" + "    },
" + "    {
" + "      "appChannel": "umeng",
" + "      "appId": "tianya",
" + "      "appPlatform": "winphone",
" + "      "appVersion": "1.0.0",
" + "      "deviceStyle": "vivo 3",
" + "      "osType": "mi 5.5",
" + "      "tenantId": "tnt023"
" + "    },
" + "    {
" + "      "appChannel": "umeng",
" + "      "appId": "sohuvideo",
" + "      "appPlatform": "android",
" + "      "appVersion": "1.0.0",
" + "      "deviceStyle": "iphone 6 plus",
" + "      "osType": "mi 5.5",
" + "      "tenantId": "tnt023"
" + "    }
" + "  ]
" + "}" ;
        out.write(json.getBytes());
        out.flush();
        out.close();
        int code = conn.getResponseCode() ;
        if(code == 200){
            System.out.println("发送ok!!");
        }
    }
}

原文地址:https://www.cnblogs.com/share23/p/9864937.html