11-4 12 Nginx安装 默认虚拟主机 用户认证 域名重定向

12.6 Nginx安装javascript

12.7 默认虚拟主机php

12.8 Nginx用户认证css

12.9 Nginx域名重定向html

扩展前端

nginx.conf 配置详解 http://www.ha97.com/5194.htmljava

http://my.oschina.net/duxuefeng/blog/34880node

12.6 Nginx安装

  1. 下载
  2. 安装
  3. 配置
  4. 启动服务

第一步:下载nginx

[root@axiang-02 nginx]# cd /usr/local/src
[root@axiang-02 src]# wget http://nginx.org/download/nginx-1.12.1.tar.gz
[root@axiang-02 src]#  tar zxf nginx-1.12.1.tar.gz

第二步:安装apache

[root@axiang-02 src]# cd nginx-1.12.1/
[root@axiang-02 nginx-1.12.1]# ./configure --prefix=/usr/local/nginx ^C
#若是须要支持某些模块,能够再次添加,好比https ssl等
[root@axiang-02 nginx-1.12.1]# make &&  make install
[root@axiang-02 nginx-1.12.1]# echo $?

第三步:配置vim

[root@axiang-02 nginx-1.12.1]# cd /usr/local/nginx/conf/
[root@axiang-02 conf]# mv nginx.conf nginx.conf.bak ^C
[root@axiang-02 conf]# vim nginx.conf //D15Z/nginx.conf

user nobody nobody;
#定义启动Nginx的用户
worker_processes 2;
#定义子进程数目
error_log /usr/local/nginx/logs/nginx_error.log crit;
pid /usr/local/nginx/logs/nginx.pid;
worker_rlimit_nofile 51200;
#指定Nginx最多可打开的文件数目
events
{
    use epoll;
    worker_connections 6000;
    #进程最大链接数
}

http
{
    include mime.types;
    default_type application/octet-stream;
    server_names_hash_bucket_size 3526;
    server_names_hash_max_size 4096;
    log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_local]'
    ' $host "$request_uri" $status'
    ' "$http_referer" "$http_user_agent"';
    sendfile on;
    tcp_nopush on;
    keepalive_timeout 30;
    client_header_timeout 3m;
    client_body_timeout 3m;
    send_timeout 3m;
    connection_pool_size 256;
    client_header_buffer_size 1k;
    large_client_header_buffers 8 4k;
    request_pool_size 4k;
    output_buffers 4 32k;
    postpone_output 1460;
    client_max_body_size 10m;
    client_body_buffer_size 256k;
    client_body_temp_path /usr/local/nginx/client_body_temp;
    proxy_temp_path /usr/local/nginx/proxy_temp;
    fastcgi_temp_path /usr/local/nginx/fastcgi_temp;
    fastcgi_intercept_errors on;
    tcp_nodelay on;
    gzip on;
    gzip_min_length 1k;
    gzip_buffers 4 8k;
    gzip_comp_level 5;
    gzip_http_version 1.1;
    gzip_types text/plain application/x-javascript text/css text/htm 
    application/xml;
    server
    #虚拟主机
    {
        listen 80;
        server_name localhost;
        index index.html index.htm index.php;
        root /usr/local/nginx/html;
        location ~ \.php$
        #配置PHP解析
        {
            include fastcgi_params;
            fastcgi_pass unix:/tmp/php-fcgi.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
        }    
    }
}

第四步:启动服务

[root@axiang-02 conf]# vim /etc/init.d/nginx   //建立启动服务脚本 D15Z/etc_init.d_nginx

#!/bin/bash
# chkconfig: - 30 21
# description: http service.
# Source Function Library
. /etc/init.d/functions
# Nginx Settings
NGINX_SBIN="/usr/local/nginx/sbin/nginx"
NGINX_CONF="/usr/local/nginx/conf/nginx.conf"
NGINX_PID="/usr/local/nginx/logs/nginx.pid"
RETVAL=0
prog="Nginx"
start() 
{
    echo -n $"Starting $prog: "
    mkdir -p /dev/shm/nginx_temp
    daemon $NGINX_SBIN -c $NGINX_CONF
    RETVAL=$?
    echo
    return $RETVAL
}
stop() 
{
    echo -n $"Stopping $prog: "
    killproc -p $NGINX_PID $NGINX_SBIN -TERM
    rm -rf /dev/shm/nginx_temp
    RETVAL=$?
    echo
    return $RETVAL
}
reload()
{
    echo -n $"Reloading $prog: "
    killproc -p $NGINX_PID $NGINX_SBIN -HUP
    RETVAL=$?
    echo
    return $RETVAL
}
restart()
{
    stop
    start
}
configtest()
{
    $NGINX_SBIN -c $NGINX_CONF -t
    return 0
}
case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  reload)
        reload
        ;;
  restart)
        restart
        ;;
  configtest)
        configtest
        ;;
  *)
        echo $"Usage: $0 {start|stop|reload|restart|configtest}"
        RETVAL=1
esac
exit $RETVAL

[root@axiang-02 conf]# chmod 755 /etc/init.d/nginx 
[root@axiang-02 conf]# chkconfig --add nginx
[root@axiang-02 conf]# chkconfig nginx on
[root@axiang-02 conf]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@axiang-02 conf]# /etc/init.d/nginx start
[root@axiang-02 conf]# netstat -lntp | grep 80
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      1122/nginx: master

Nginx安装完成!

  • Ss为父进程,通常为root下
  • S为子进程

测试

vi /usr/local/nginx/html/1.php 
     <?php
    echo "This is nginx page.";
    ?>
     curl localhost/1.php

常见的502问题解决

对于LNMP来讲,最多见的就是502问题,LNMP环境搭建完成后,一访问网站直接提示“502 Bad Gateway”。主要缘由大体分为两种:

(1)配置错误

在Nginx配置中有这么一段:

location ~ .php$ #配置PHP解析 { include fastcgi_params; fastcgi_pass unix:/tmp/php-fcgi.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name; } 若是把fastcgi_pass(这是用来通讯的)后面指定的路径配置错了,那么就会出现502错误,由于Nginx找不到php-fpm了,fastcgi _pass后面能够跟socket也能够跟IP:port,默认监听地址为:127.0.0.1:9000。 注意: 这里用两种形式均可以,可是两个配置文件(Nginx和php-fpm)中的形式必定要统一,否则绝对502;若是用套接字形式的话,socket文件的路径必定要对,否则也仍是502。

(2)资源耗尽

LNMP架构处理PHP时,是Nginx直接调取后端的php-fpm服务,若是Nginx的请求量偏高,而咱们又没给php-fpm配置足够的子进程数,那么总有php-fpm资源耗尽的时候,一旦耗尽Nginx找不到php-fpm,此时也会致使502错误出现。解决办法就是调整php-fpm.conf中的pm.max_children数值,使其增长。但也不能无限制增长,由于服务器的资源有限。4G内存机器若是只跑php-fpm和Nginx,不跑MySQL服务,pm.max _children能够设置为150,尽可能不要超过该数值,8G内存设置为300,以此类推。

若是遇到其它的较为少见的错误,咱们能够修改nginx的错误日志(/usr/local/nginx/logs/nginx_error.log)的级别,在配置文件/usr/local/nginx/conf/nginx.conf中将crit改成debug,使其记录最多的日志内容,这样方便咱们排查错误,可是配置更改完成后要记得将级别改回crit,避免日志文件占用太多磁盘空间。

12.7 Nginx默认虚拟主机

[root@axiang-02 conf]# vim nginx.conf
删除下面的server配置,加入一行:
include vhost/*.conf;

[root@axiang-02 conf]# mkdir vhost
[root@axiang-02 conf]cd !$;  vim aaa.conf //加入如下内容
server
{
    listen 80 default_server;  // 有这个标记的就是默认虚拟主机
    server_name aaa.com;
    index index.html index.htm index.php;
    root /data/wwwroot/aaa;
}
[root@axiang-02 vhost]# mkdir -p /data/wwwroot/aaa/
[root@axiang-02 vhost]# echo “This is a default site.”>/data/wwwroot/aaa/index.html
[root@axiang-02 vhost]# /usr/local/nginx/sbin/nginx -t
[root@axiang-02 vhost]# /usr/local/nginx/sbin/nginx -s reload
[root@axiang-02 vhost]# curl localhost
[root@axiang-02 vhost]# curl -x127.0.0.1:80 23sdsa.com

  • 若是没有default_server,则按字符顺序寻找vhost下虚拟主机,例如 aaa在bbb前
  • 若是vhost定义了两个default_server,则报错
  • 直接curl aaa.com 没有返回
  • 路径写错好比wwwoot则404!

12.8 Nginx用户认证

[root@axiang-02 vhost]# vim bbb.conf  //加入以下内容
...
server
{
    listen 80 default_server;  //注意只能定义一个默认虚拟主机,须要把aaa的删掉
    server_name bbb.com;
    index index.html index.htm index.php;
    root /data/wwwroot/bbb.com;

location  /
    {
        auth_basic              "Auth";
        auth_basic_user_file   /usr/local/nginx/conf/htpasswd;
    }
}
...
 yum install -y httpd  
 htpasswd -c /usr/local/nginx/conf/htpasswd ax  //也能够用apache2.4/bin下的htpasswd生成

-t && -s reload //测试配置并从新加载

  • -t测试不经过没法reload

定义了多个默认虚拟主机产生冲突

域名冲突,检查其余conf是否重复定义同一个域名

[root@axiang-02 vhost]# mkdir /data/wwwroot/bbb.com
[root@axiang-02 vhost]# echo “This is bbb.com page”>/data/wwwroot/bbb.com/index.html
[root@axiang-02 vhost]# curl -x127.0.0.1:80 bbb.com -I//状态码为401说明须要验证
[root@axiang-02 vhost]# curl -uaming:passwd 访问状态码变为200

针对目录及页面的用户认证

location  /admin/
location ~ admin.php  //没有该页面也会提示401,输入用户密码则404

12.9 Nginx域名重定向

更改ccc.conf

server
{
    listen 80;
    server_name ccc.com ccc1.com ccc2.com;
    index index.html index.htm index.php;
    root /data/wwwroot/ccc.com;
    if ($host != 'test.com' )
    {
        rewrite  ^/(.*)$  http://ccc.com/$1  permanent;
    }
}

测试效果

  • server_name后面支持写多个域名,httpd写在server_alias后面
  • permanent为永久重定向,状态码为301,SEO权重高,若是写redirect则为302
  • 谓默认虚拟主机就是/usr/local/nginx/conf/vhost目录下虚拟主机配置文件中有“default_server”标记的虚拟主机

扩展:Nginx配置文件详解

#定义Nginx运行的用户和用户组
user www www;

#nginx进程数,建议设置为等于CPU总核心数。
worker_processes 8;

#全局错误日志定义类型,[ debug | info | notice | warn | error | crit ]
error_log /var/log/nginx/error.log info;

#进程文件
pid /var/run/nginx.pid;

#一个nginx进程打开的最多文件描述符数目,理论值应该是最多打开文件数(系统的值ulimit -n)与nginx进程数相除,可是nginx分配请求并不均匀,因此建议与ulimit -n的值保持一致。
worker_rlimit_nofile 65535;

#工做模式与链接数上限
events
{
#参考事件模型,use [ kqueue | rtsig | epoll | /dev/poll | select | poll ]; epoll模型是Linux 2.6以上版本内核中的高性能网络I/O模型,若是跑在FreeBSD上面,就用kqueue模型。
use epoll;
#单个进程最大链接数(最大链接数=链接数*进程数)
worker_connections 65535;
}

#设定http服务器
http
{
include mime.types; #文件扩展名与文件类型映射表
default_type application/octet-stream; #默认文件类型
#charset utf-8; #默认编码
server_names_hash_bucket_size 128; #服务器名字的hash表大小
client_header_buffer_size 32k; #上传文件大小限制
large_client_header_buffers 4 64k; #设定请求缓
client_max_body_size 8m; #设定请求缓
sendfile on; #开启高效文件传输模式,sendfile指令指定nginx是否调用sendfile函数来输出文件,对于普通应用设为 on,若是用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络I/O处理速度,下降系统的负载。注意:若是图片显示不正常把这个改为off。
autoindex on; #开启目录列表访问,合适下载服务器,默认关闭。
tcp_nopush on; #防止网络阻塞
tcp_nodelay on; #防止网络阻塞
keepalive_timeout 120; #长链接超时时间,单位是秒

#FastCGI相关参数是为了改善网站的性能:减小资源占用,提升访问速度。下面参数看字面意思都能理解。
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;

#gzip模块设置
gzip on; #开启gzip压缩输出
gzip_min_length 1k; #最小压缩文件大小
gzip_buffers 4 16k; #压缩缓冲区
gzip_http_version 1.0; #压缩版本(默认1.1,前端若是是squid2.5请使用1.0)
gzip_comp_level 2; #压缩等级
gzip_types text/plain application/x-javascript text/css application/xml;
#压缩类型,默认就已经包含text/html,因此下面就不用再写了,写上去也不会有问题,可是会有一个warn。
gzip_vary on;
#limit_zone crawler $binary_remote_addr 10m; #开启限制IP链接数的时候须要使用

upstream blog.ha97.com {
#upstream的负载均衡,weight是权重,能够根据机器配置定义权重。weigth参数表示权值,权值越高被分配到的概率越大。
server 192.168.80.121:80 weight=3;
server 192.168.80.122:80 weight=2;
server 192.168.80.123:80 weight=3;
}

#虚拟主机的配置
server
{
#监听端口
listen 80;
#域名能够有多个,用空格隔开
server_name www.ha97.com ha97.com;
index index.html index.htm index.php;
root /data/www/ha97;
location ~ .*\.(php|php5)?$
{
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
#图片缓存时间设置
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 10d;
}
#JS和CSS缓存时间设置
location ~ .*\.(js|css)?$
{
expires 1h;
}
#日志格式设定
log_format access '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $http_x_forwarded_for';
#定义本虚拟主机的访问日志
access_log /var/log/nginx/ha97access.log access;

#对 "/" 启用反向代理
location / {
proxy_pass http://127.0.0.1:88;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
#后端的Web服务器能够经过X-Forwarded-For获取用户真实IP
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#如下是一些反向代理的配置,可选。
proxy_set_header Host $host;
client_max_body_size 10m; #容许客户端请求的最大单文件字节数
client_body_buffer_size 128k; #缓冲区代理缓冲用户端请求的最大字节数,
proxy_connect_timeout 90; #nginx跟后端服务器链接超时时间(代理链接超时)
proxy_send_timeout 90; #后端服务器数据回传时间(代理发送超时)
proxy_read_timeout 90; #链接成功后,后端服务器响应时间(代理接收超时)
proxy_buffer_size 4k; #设置代理服务器(nginx)保存用户头信息的缓冲区大小
proxy_buffers 4 32k; #proxy_buffers缓冲区,网页平均在32k如下的设置
proxy_busy_buffers_size 64k; #高负荷下缓冲大小(proxy_buffers*2)
proxy_temp_file_write_size 64k;
#设定缓存文件夹大小,大于这个值,将从upstream服务器传
}

#设定查看Nginx状态的地址
location /NginxStatus {
stub_status on;
access_log on;
auth_basic "NginxStatus";
auth_basic_user_file conf/htpasswd;
#htpasswd文件的内容能够用apache提供的htpasswd工具来产生。
}

#本地动静分离反向代理配置
#全部jsp的页面均交由tomcat或resin处理
location ~ .(jsp|jspx|do)?$ {
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://127.0.0.1:8080;
}
#全部静态文件由nginx直接读取不通过tomcat或resin
location ~ .*.(htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma)$
{ expires 15d; }
location ~ .*.(js|css)?$
{ expires 1h; }
}
}

本节来自:http://www.ha97.com/5194.html 详参:http://wiki.nginx.org/Main

相关文章
相关标签/搜索