rokevin
移动
前端
语言
  • 基础

    • Linux
    • 实施
    • 版本构建
  • 应用

    • WEB服务器
    • 数据库
  • 资讯

    • 工具
    • 部署
开放平台
产品设计
  • 人工智能
  • 云计算
计算机
其它
GitHub
移动
前端
语言
  • 基础

    • Linux
    • 实施
    • 版本构建
  • 应用

    • WEB服务器
    • 数据库
  • 资讯

    • 工具
    • 部署
开放平台
产品设计
  • 人工智能
  • 云计算
计算机
其它
GitHub
  • nginx

nginx

nginx 官网

http://nginx.org/

nginx安装和配置

搜索nginx

brew search nginx

常见问题

问题 日志 nginx支持WebScoket

安装nginx

brew install nginx

安装完后,在终端显示的信息

/usr/local/etc/nginx/nginx.conf (配置文件路径)
/usr/local/var/www (服务器默认路径)
/usr/local/Cellar/nginx/1.10.1 (安装路径)

==> Caveats
Docroot is: /usr/local/var/www

The default port has been set in /usr/local/etc/nginx/nginx.conf to 8080 so that
nginx can run without sudo.

nginx will load all files in /usr/local/etc/nginx/servers/.

To have launchd start nginx now and restart at login:
  brew services start nginx
Or, if you don't want/need a background service you can just run:
  nginx  
==> Summary  
🍺  /usr/local/Cellar/nginx/1.10.1: 7 files, 972.2K

nginx 涉及到的目录

/usr/local/bin/nginx
/usr/local/Cellar/nginx
/usr/local/Cellar/nginx/1.10.1/bin/nginx
/usr/local/etc/nginx
/usr/local/Library/LinkedKegs/nginx
/usr/local/opt/nginx
/usr/local/var/log/nginx
/usr/local/var/run/nginx

访问localhost:8080,成功说明安装好了

简单命令

启动
nginx
start nginx
退出
nginx -s quit
nginx -s stop
nginx.conf配置
nginx -t
重新加载
nginx -s reload

nginx 安装2

方法1

CentOS 7 yum 安装nginx

1.添加Nginx到YUM源

添加CentOS 7 Nginx yum资源库,打开终端,使用以下命令:

sudo rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm

2.安装Nginx

在你的CentOS 7 服务器中使用yum命令从Nginx源服务器中获取来安装Nginx:

sudo yum install -y nginx

Nginx将完成安装在你的CentOS 7 服务器中。

3.启动Nginx

刚安装的Nginx不会自行启动。运行Nginx:

sudo systemctl start nginx.service

如果一切进展顺利的话,现在你可以通过你的域名或IP来访问你的Web页面来预览一下Nginx的默认页面;

nginx_default

如果看到这个页面,那么说明你的CentOS 7 中 web服务器已经正确安装。

CentOS 7 开机启动Nginx sudo systemctl enable nginx.service 更多systemctl命令可查看《systemctl命令用法》

Nginx配置信息 网站文件存放默认目录

/usr/share/nginx/html 网站默认站点配置

/etc/nginx/conf.d/default.conf 自定义Nginx站点配置文件存放目录

/etc/nginx/conf.d/ Nginx全局配置

/etc/nginx/nginx.conf Nginx启动

nginx -c nginx.conf

在这里你可以改变设置用户运行Nginx守护程序进程一样,和工作进程的数量得到了Nginx正在运行,等等。 Linux查看公网IP 您可以运行以下命令来显示你的服务器的公共IP地址:

ip addr show eth0 | grep inet | awk '{ print 2; }' | sed 's/\/.*//'


方法2

1.安装依赖库

nginx的一些模块依赖一些lib库,所以在安装nginx之前,必须先安装这些依赖库 g++、gcc、openssl-devel、pcre-devel和zlib-devel 执行如下命令安装

$   yum install gcc-c++  
$   yum install pcre pcre-devel  
$   yum install zlib zlib-devel  
$   yum install openssl openssl--devel

2.下载nginx,并安装

$ wget http://nginx.org/download/nginx-1.12.1.tar.gz

3.解压nginx压缩包

$ tar -zxvf nginx-1.12.1.tar.gz 

4.在安装目录依次执行./configure、make、make install

$ ./configure

显示如下:

Configuration summary
  + using system PCRE library
  + OpenSSL library is not used
  + using system zlib library

  nginx path prefix: "/usr/local/nginx"
  nginx binary file: "/usr/local/nginx/sbin/nginx"
  nginx modules path: "/usr/local/nginx/modules"
  nginx configuration prefix: "/usr/local/nginx/conf"
  nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
  nginx pid file: "/usr/local/nginx/logs/nginx.pid"
  nginx error log file: "/usr/local/nginx/logs/error.log"
  nginx http access log file: "/usr/local/nginx/logs/access.log"
  nginx http client request body temporary files: "client_body_temp"
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"
$ make
...
...
objs/src/http/modules/ngx_http_upstream_least_conn_module.o \
objs/src/http/modules/ngx_http_upstream_keepalive_module.o \
objs/src/http/modules/ngx_http_upstream_zone_module.o \
objs/ngx_modules.o \
-ldl -lpthread -lcrypt -lpcre -lz \
-Wl,-E
sed -e "s|%%PREFIX%%|/usr/local/nginx|" \
        -e "s|%%PID_PATH%%|/usr/local/nginx/logs/nginx.pid|" \
        -e "s|%%CONF_PATH%%|/usr/local/nginx/conf/nginx.conf|" \
        -e "s|%%ERROR_LOG_PATH%%|/usr/local/nginx/logs/error.log|" \
        < man/nginx.8 > objs/nginx.8
make[1]: Leaving directory `/usr/nginx/nginx-1.12.1'

$ make install
...
...

        || cp conf/scgi_params '/usr/local/nginx/conf'
cp conf/scgi_params \
        '/usr/local/nginx/conf/scgi_params.default'
test -f '/usr/local/nginx/conf/nginx.conf' \
        || cp conf/nginx.conf '/usr/local/nginx/conf/nginx.conf'
cp conf/nginx.conf '/usr/local/nginx/conf/nginx.conf.default'
test -d '/usr/local/nginx/logs' \
        || mkdir -p '/usr/local/nginx/logs'
test -d '/usr/local/nginx/logs' \
        || mkdir -p '/usr/local/nginx/logs'
test -d '/usr/local/nginx/html' \
        || cp -R html '/usr/local/nginx'
test -d '/usr/local/nginx/logs' \
        || mkdir -p '/usr/local/nginx/logs'
make[1]: Leaving directory `/usr/nginx/nginx-1.12.1'

5.测试是否安装成功

$ whereis nginx

默认安装目录:

/usr/local/nginx

配置

1.反向代理

http://blog.csdn.net/zjx86320/article/details/51730388

2.高性能负载均衡集群 http://blog.csdn.net/wang379275614/article/details/47778201

location /css { root /hlvan/project-web/hm-admin/target; }

location /js {
        root /jenkins/project-web/hm-admin/target;
}


location /images {
        root /jenkins/project-web/hm-admin/target;
}


location /html {
        root /jenkins/project-web/hm-admin/target;
}

location = /index.html {
        root /jenkins/project-web/hm-admin/target;
}

Basic Auth认证

https://www.jianshu.com/p/b4a78af4e266

模块

status统计

限制请求源域名

if ($http_referer ~* "63w6b.cn") { return 404; }

nginx限制请求数(ngx_http_limit_req_module) 实现详解

https://blog.csdn.net/huzilinitachi/article/details/79694641

ngx_http_limit_req_module

http://www.ttlsa.com/nginx/nginx-limiting-the-number-of-requests-ngx_http_limit_req_module-module/

https://blog.csdn.net/wanglei_storage/article/details/51076882

在http模块配置:

http {
    ...
    # 一秒钟执行一个请求
    limit_req_zone $binary_remote_addr zone=hlvan:10m rate=1r/s;
}

在http、server、location可以配置:

limit_req zone=hlvan burst=3 nodelay;

burst 相当于一个管道,后面的请求会在里面排队

nodelay 增加此字段则超过burst的都会返回503不延后请求

location ^~ /tyb/merchant_web/order/list {
    limit_req zone=hlvan burst=3 nodelay;
    proxy_set_header X-Forwarded-For $remote_addr;
    proxy_pass http://127.0.0.1:21200/merchant_web/order/list;
    proxy_set_header Host $host:$server_port;
    access_log /var/log/nginx/tyb.hlvan.cn.access.log post_format;
}

nginx配置proxy_pass URL末尾加与不加/(斜线)的区别

https://blog.csdn.net/shichen501/article/details/81003099

nginx 网站迁移配置

server {
        server_name api.123123.cn;
        listen 80;
        access_log off;
        client_max_body_size 10M;

# 配置此项用于重定向请求 $1 表示带参数 如http://xxx/abc?bcd=123 permanent 表示永久重定向
rewrite ^/(.*) http://openapi.456456.cn/$1 permanent;

        location / {
                proxy_pass http://rokevin/open_api/;
                access_log /var/log/nginx/api.123123.cn.access.log post_format;
        }
}

nginx命令

查看Nginx的版本号:nginx -V

启动Nginx:start nginx

快速停止或关闭Nginx:nginx -s stop

正常停止或关闭Nginx:nginx -s quit

配置文件修改重装载命令:nginx -s reload

最近更新:: 2020/7/26 21:46
Contributors: luokaiwen