nginx安装,虚拟主机,用户认证及域名重定向

nginx安装

  • cd /usr/local/src/
  • wget http://nginx.org/download/nginx-1.14.0.tar.gz
  • tar zxfv nginx-1.14.0.tar.gz
  • cd nginx-1.14.0/
  • ./configure --prefix=/usr/local/nginx
  • make && make install
  • 启动文件配置vim /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
  • 开机启动
    • chmod 755 /etc//init.d/nginx
    • chkconfig --add nginx
    • chkconfig nginx on
  • nginx的配置文件
    [[email protected] conf]# mv nginx.conf nginx.conf.bak
    [[email protected] conf]# vim nginx.conf

    参考

    user nobody nobody;      #定义Nginx运行的用户和用户组
    worker_processes 2;      #nginx进程数,建议设置为等于CPU总核心数
    error_log /usr/local/nginx/logs/nginx_error.log crit;  #全局错误日志定义类型
    pid /usr/local/nginx/logs/nginx.pid;  #进程文件
    worker_rlimit_nofile 51200;   #一个nginx进程打开的最多文件描述符数目,理论值应该是最多打开文件数(系统的值ulimit -n)与nginx进程数相除,但是nginx分配请求并不均匀,所以建议与ulimit -n的值保持一致。
    events          #工作模式与连接数上限
    {
    use epoll;
    worker_connections 6000; #单个进程最大连接数(最大连接数=连接数*进程数)
    }
    http   #设定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_pass 127.0.0.1:9000;  #指定监听的sockter或者Ip加端口
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
        }
    }
    }
  • 检测配置文件/usr/local/nginx/sbin/nginx -t
  • 重新加载配置文件/usr/local/nginx/sbin/nginx -s reload
  • 测试
    • curl localhost
  • 测试php解析
    [[email protected] ~]# vim /usr/local/nginx/html/2.php
    <?php
    echo "test php scripts.";
    ?>
    [[email protected] ~]# curl localhost/2.php
    test php scripts.

    nginx的默认虚拟主机

  • 在/usr/local/nginx/conf/nginx.conf的http服务里面增加一行 include vhosts/*.conf;
  • mkdir /usr/local/nginx/conf/vhost
  • vim /usr/local/nginx/conf/default.conf
    server
    {
    listen 80 default_server;
    server_name aaa.com
    index index.html index.htm index.php;
    root /data/wwwroot/default;
    }
  • mkdir -p /data/wwwroot/default
  • 创建测试页
    [[email protected] vhosts]# vim /data/wwwroot/default/index.html
    this is a default site.
  • /usr/local/nginx/sbin/nginx -t
  • /usr/local/nginx/sbin/nginx -s reload
  • 测试
    [[email protected] vhosts]# curl localhost
    this is a default site.
    [[email protected] vhosts]# curl -x 127.0.0.1:80 aaa.com
    this is a default site.
    [[email protected] vhosts]# curl -x 127.0.0.1:80 bbb.com
    this is a default site.

    nginx的用户认证

  • vim /usr/local/nginx/conf/vhosts/test.com.conf
    server
    {
    listen 80;
    server_name test.com;
    index index.html index.htm index.php;
    root /data/wwwroot/test.com;
    
    location /   #/针对整个网站,/admin针对admin目录,~admin.php针对一个页面
    {
      auth_basic     "Auth"  #指定用户名
      auth_basic_user_file  /usr/local/nginx/conf/htpasswd; #指定密码文件
    }
    }
  • yum install -y httpd 安装apche
    Downloading packages:
    Error downloading packages:
    mailcap-2.1.41-2.el7.noarch: [Errno 5] [Errno 12] Cannot allocate memory
    httpd-tools-2.4.6-80.el7.centos.x86_64: [Errno 5] [Errno 12] Cannot allocate memory
    apr-util-1.5.2-6.el7.x86_64: [Errno 5] [Errno 12] Cannot allocate memory
    httpd-2.4.6-80.el7.centos.x86_64: [Errno 5] [Errno 12] Cannot allocate memory
    apr-1.4.8-3.el7_4.1.x86_64: [Errno 5] [Errno 12] Cannot allocate memory
    这个错误需要手动安装不能安装的包,从下网上一个个安装
  • 生产密码文件
    [[email protected] ~]# htpasswd -c /usr/local/nginx/conf/htpasswd aming
    New password:
    Re-type new password:
    Adding password for user aming
    [[email protected] ~]# cat /usr/local/nginx/conf/htpasswd
    aming:$apr1$Th4uOUg/$tKN0B6BveSJ2.DtPu4Yfl.
    [[email protected] ~]# htpasswd  /usr/local/nginx/conf/htpasswd aming01
    New password:
    Re-type new password:
    Adding password for user aming01
    [[email protected] ~]# cat /usr/local/nginx/conf/htpasswd
    aming:$apr1$Th4uOUg/$tKN0B6BveSJ2.DtPu4Yfl.
    aming01:$apr1$6aaY/ylb$9eUuW8lFsxlbzcnVQUHvq1
  • 重新加载配置文件
    [[email protected]aminglinux-02 ~]# /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
    [[email protected] ~]# /usr/local/nginx/sbin/nginx -s reload
  • 测试
    [[email protected] ~]# curl -u aming:123456 -x127.0.0.1:80 test.com
    test.com

    域名重定向

  • 更改test.com.conf
    server
    {
    listen 80;
    server_name test.com test1.com test2.com; #指定多个域名,httpd不一样
    index index.html index.htm index.php;
    root /data/wwwroot/test.com;
    if ($host != ‘test.com‘) {
     rewrite  ^/(.*)$ http://test.com/$1 permanent; #permanent是301,redirect是3
    02
    }
    }
  • 加载配置
    [[email protected] ~]# /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
    [[email protected] ~]# /usr/local/nginx/sbin/nginx -s reload
  • 测试
    [[email protected] ~]# curl -x127.0.0.1:80 test1.com/index.html/daad -I
    HTTP/1.1 301 Moved Permanently
    Server: nginx/1.6.2
    Date: Fri, 08 Jun 2018 01:39:47 GMT
    Content-Type: text/html
    Content-Length: 184
    Connection: keep-alive
    Location: http://test.com/index.html/daad
    [[email protected] ~]# curl -x127.0.0.1:80 test2.com/index.html/daad -I
    HTTP/1.1 301 Moved Permanently
    Server: nginx/1.6.2
    Date: Fri, 08 Jun 2018 01:39:53 GMT
    Content-Type: text/html
    Content-Length: 184
    Connection: keep-alive
    Location: http://test.com/index.html/daad
    [[email protected] ~]# curl -x127.0.0.1:80 test3.com/index.html/daad -I
    HTTP/1.1 404 Not Found
    Server: nginx/1.6.2
    Date: Fri, 08 Jun 2018 01:39:58 GMT
    Content-Type: text/html
    Content-Length: 168
    Connection: keep-alive

    #扩展

  • nginx.conf 配置详解( http://my.oschina.net/duxuefeng/blog/34880)
  • nginx rewrite四种flag(http://unixman.blog.51cto.com/10163040/1711943)

原文地址:http://blog.51cto.com/akui2521/2126232

时间: 2024-11-03 05:51:45

nginx安装,虚拟主机,用户认证及域名重定向的相关文章

LAMP架构(nginx安装,默认虚拟主机,用户认证,域名重定向,nginx配置文件详解)

一.安装nginx [[email protected] conf]# wget http://nginx.org/download/nginx-1.8.0.tar.gz [[email protected] conf]# tar zxvf nginx-1.8.0.tar.gz [[email protected] conf]# cd nginx-1.8.0 [[email protected] conf]# ./configure --prefix=/usr/local/nginx [[ema

Nginx安装与配置:默认虚拟主机、用户认证和域名重定向

一.Nginx安装 1.下载并解压安装包 [[email protected] ~]# cd /usr/local/src/ [[email protected] src]# wget http://nginx.org/download/nginx-1.12.2.tar.gz [[email protected] src]# tar zxf nginx-1.12.2.tar.gz 2.配置编译选项 [[email protected] src]# cd nginx-1.12.2 [[email 

90.Nginx安装与配置:默认虚拟主机、用户认证和域名重定向

一.Nginx安装 1.下载并解压安装包 [[email protected] ~]# cd /usr/local/src/[[email protected] src]# wget http://nginx.org/download/nginx-1.12.2.tar.gz[[email protected] src]# tar zxf nginx-1.12.2.tar.gz 2.配置编译选项 [[email protected] src]# cd nginx-1.12.2[[email pro

LNMP第二部分nginx、php配置(用户认证、域名重定向、日志、配置缓存、防盗链)

一.nginx的配置( nginx.conf) 1.nginx的主配置文件位置: /usr/local/nginx/conf/nginx.con 2.清空  /usr/local/nginx/conf/nginx.con默认的配置文件内容 [[email protected] ~]# > /usr/local/nginx/conf/nginx.conf >:重定向的意思,单独使用,可以把一个文本文档快速清空 3.拷贝一下代码到/usr/local/nginx/conf/nginx.conf文件

2018-3-13 12周2次课 Nginx安装、默认虚拟主机、用户认证、域名重定向

12.6 Nginx安装 [[email protected] ~]# cd /usr/local/src/ [[email protected] src]# wget http://nginx.org/download/nginx-1.12.2.tar.gz (过程省略) [[email protected] src]# tar zxvf nginx-1.12.2.tar.gz [[email protected] src]# cd nginx-1.12.2/ [[email protecte

Nginx安装、默认虚拟主机、用户认证、域名重定向

Nginx安装 cd到 /usr/local/src/ 目录cd /usr/local/src/ 下载Nginx源码包wget http://nginx.org/download/nginx-1.12.1.tar.gz 解压源码包tar zxvf nginx-1.12.1.tar.gz 进入源码包目录cd nginx-1.12.1 进行编译(这里我们没有加什么参数,但是如果有需要就可以在这一步加上参数)./configure --prefix=/usr/local/nginx 然后makemak

Nginx配置——虚拟主机基于IP,域名,端口(实战!)

Nginx虚拟主机 基于域名的虚拟主机 基于IP地址的虚拟主机 基于端口的虚拟主机 一,安装DNS域名解析服务器 1,安装bind服务器 [[email protected] ~]# yum install bind -y 2,修改主配置文件(named.conf) [[email protected] ~]# vim /etc/named.conf options { listen-on port 53 { any; }; ##监听所有 listen-on-v6 port 53 { ::1;

nginx安装+虚拟主机配置

安装 (1)在线安装 $sudo apt-get install nginx Nginx的版本是1.2.1 ubuntu安装Nginx之后的文件结构大致为: 所有的配置文件都在/etc/nginx下,并且每个虚拟主机已经安排在了/etc/nginx/sites-available下 启动程序文件在/usr/sbin/nginx 日志放在了/var/log/nginx中,分别是access.log和error.log 并已经在/etc/init.d/下创建了启动脚本nginx 默认的虚拟主机的目录

Nginx安装配置实现用户认证、反向代理、隐藏版本号

一.Nginx安装 1.检查并且安装依赖组件           检查安装nginx的模块需要第三方库的支持,检查是否安装下列库:zlib.zlib-devel.openssl.openssl-devel.pcre.pcre-devel如果没有,则全部装上          # rpm -qa | grep pcre  ##没有任何信息则没安装 2.安装pcre,pcre-devel # tar -zxvf pcre-6.6.9.tar.gz          # cd pcre-6.6.9/