博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【运维】nginx服务器基本配置指南
阅读量:7082 次
发布时间:2019-06-28

本文共 4702 字,大约阅读时间需要 15 分钟。

前言

Nginx (engine x) 是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP服务器

安装

  • yum安装
  • 源码安装

yum安装

以CentOS6.5 为例

设置yum源, vi etc/yum.repos.d/nginx.repo

[nginx]name=nginx repobaseurl=http://nginx.org/packages/mainline/centos/6/x86_64/gpgcheck=0enabled=1

设置代理 vi /etc/yum.conf (公网情况下可忽略)

proxy=代理IP

命令安装

sudo yum install nginx

源码安装

wget http://nginx.org/download/nginx-1.13.12.tar.gztar -xvzf nginx-1.13.12.tar.gzcd nginx-1.13.12./configure --prefix=/usr/local/nginxmakesudo make install

模块扩展安装

在安装nginx的时候, 往往会有配置需求, 同时如果我们想要安装一个第三方的模块, 则需要 --add-module 进行设置. 例如下面要求

已知第三方模块

  • 前端JS,CSS文件合并资源
git clone https://github.com/alibaba/nginx-http-concat.git# nginx 编译  ./configure --add-module=/path/to/nginx-http-concatmake sudo make install

源码重新编译

有时候, 我们需要给现有的nginx加入一些扩展模块. 那么就需要通过对应的版本的源代码, 重新configure, 具体步骤如下:

# 老的nginx 执行 nginx -V/usr/local/nginx/sbin/nginx -V# nginx 同一版本下的源码 执行 configure, 附加新的模块./configure --prefix=/usr/local/nginx --add-module=/path/to/nginx-http-concat# 编译make# 备份老的nginxcp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.old# 将objs目录下产生新的 nginx 覆盖老的nginxcp objs/nginx /usr/local/nginx/sbin/nginx# 如果出现文件打开错误,运行下面这句mkdir -p /dev/shm/nginx_temp/client_body# 测试sudo /usr/local/nginx/sbin/nginx -t# 平滑重启nginxsudo /usr/local/nginx/sbin/nginx -s reload

nginx配置

基本配置

user www www; # 用户名 用户组http {    # http服务设置          # 引入多个mime类型    include mime.types;        # 启用gzip压缩    gzip on;        # 错误页面重定向    error_page  400 401 402 403 404 405 408 410 412 413 414 415 500 501 502 503 506 = /error.html;        # 引入额外配置    include vhosts/*.conf;}

虚拟主机配置

用户请求 host为 liylblog.com 主机时, 将访问 root 目录下的资源

server {    listen       80;    server_name  liylblog.com;    root         /home/www/liyblog/html;}

路由匹配设置

server {    listen       80;    server_name  liylblog.com;    root         /home/www/liyblog/html;        # 最长优先匹配, 所以 / 为最后匹配规则    location / {        # 设置默认的index文件名称        index index.html index.html;        try_files $uri $uri/;    }        # 以.php结尾或者 .php/xxx 结尾的路径, ~ 表示大小写敏感    location ~ \.php($|/) {        # 规则    }        # 大小写敏感, 以 /img/ 开头的资源路径将被匹配    # /img/logo.png    # /img/avatar.png    location ^~ /img/ {        # 规则    }        # 大小写敏感, 匹配 /api 资源    location ^~ = /api {        f( !-e $request_filename )        {            rewrite ^/api /api/ last;        }    }}

nginx 配置负载均衡

server {    listen       80;    server_name  liylblog.com;    location / {        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_pass http://proxy_stream;    }}# 多台机器代理, 背后可能是其它web服务器upstream proxy_stream {    server 192.168.1.1 max_fails=3 fail_timeout=30s;    server 192.168.1.2 max_fails=3 fail_timeout=30s;}

反向代理

PHP-FPM代理

server {    listen       80;    server_name  liylblog.com;    root         /home/www/liyblog/html;    access_log /usr/local/nginx/logs/liylblog.access.log;    error_log  /usr/local/nginx/logs/liylblog.error.log;    location / {        index  index.html index.htm index.php;        if (!-e $request_filename) { ## if 后面必须有空格            rewrite ^(.*)$ /index.php?s=$1 last;            break;        }    }    # 大小写敏感, 匹配.php后缀结尾或者 .php/ 结尾的路径    location ~ \.php($|/) {        fastcgi_pass 127.0.0.1:9000;        fastcgi_index index.php;        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;        fastcgi_param PATH_INFO $fastcgi_script_name;        fastcgi_split_path_info ^(.+?\.php)(/.*)$;        fastcgi_param PATH_INFO $fastcgi_path_info;        client_max_body_size 100M;        fastcgi_connect_timeout 300s;        fastcgi_send_timeout 300s;        fastcgi_read_timeout 300s;        include fastcgi_params;     }}

Node.js 代理

server {    listen       80;    server_name  liylblog.com;    root         /home/www/liyblog/html;    access_log /usr/local/nginx/logs/liylblog.access.log;    error_log  /usr/local/nginx/logs/liylblog.error.log;    location @proxy {        proxy_set_header X-Real-IP $remote_addr;        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;        proxy_set_header Host $http_host;        proxy_set_header X-NginX-Proxy true;        proxy_pass http://localhost:7001;        proxy_redirect off;        proxy_http_version 1.1;        proxy_set_header Upgrade $http_upgrade;        proxy_set_header Connection "upgrade";        proxy_set_header   X-Forwarded-Proto $scheme;        proxy_cache_key sfs$request_uri$scheme;    }       location / {         autoindex on;         index  index.html index.htm;        # 尝试访问地址 例如, 访问 $root/foo, $root/foo/index.html, $root/foo/index.htm, $proxy/foo;         try_files $uri $uri/ @proxy;    }   }

小结

nginx 是一个优秀的代理服务器, 还有很多未知的知识亟待去挖掘

转载地址:http://znvml.baihongyu.com/

你可能感兴趣的文章
【Oracle】在WIN NT 64位环境下安装win64_11gR2_database。并用PL/SQL连接
查看>>
CentOS切换桌面模式和命令行模式
查看>>
noip2013火柴排序
查看>>
固定GridView的头
查看>>
ElasticSearch 监控单个节点详解
查看>>
微软职位内部推荐-Senior Development Lead
查看>>
parent对象
查看>>
三位老师
查看>>
写给测试人员:不是所有的bug都需要修复
查看>>
【转】万亿移动支付产业的难点和痛点
查看>>
c++的检测的确比C++更严格
查看>>
8月21日学习内容整理:range
查看>>
libevent总结学习
查看>>
[1041] XX easy problem
查看>>
Oracle 报错ORA-00904:标示符无效
查看>>
英语每日阅读---7、VOA慢速英语(翻译+字幕+讲解):卡梅伦呼吁女性移民学英语
查看>>
activity生命周期
查看>>
广播接收者实现IP拨号
查看>>
团队冲刺第八天
查看>>
(10/24) 图片跳坑大战--处理html中的图片
查看>>