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

[[email protected] conf]# make && make install

[[email protected] conf]# 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

[[email protected] conf]# chmod 755 /etc/init.d/nginx

[[email protected] conf]# chkconfig --add nginx

[[email protected] conf]# chkconfig nginx on

[[email protected] conf]# mv nginx.conf      nginx.conf.bak

[[email protected] conf]# vim /usr/local/nginx/conf/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;                       (pid位置)

worker_rlimit_nofile 51200;                               (最多打开多少文件)

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                                  (http服务)

{

listen 80;                                 (监听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;   (和上面一行的意思相同,只是不同的写法,监听127.0.0.1:9000)

fastcgi_index index.php;

fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;

}

}

}

[[email protected] conf]# /usr/local/nginx/sbin/nginx -t           (检查语法错误)

[[email protected] conf]# /etc/init.d/nginx start               (启动nginx)

[[email protected] conf]# netstat -lntp |grep 80                (查看80端口)

[[email protected] conf]# ps aux |grep nginx                   (查看nginx服务,可看到2个work子进程)


二、nginx默认虚拟主机

Nginx默认主机:

[[email protected] ~]# vim /usr/local/nginx/conf/nginx.conf  (删除server及下面的,在http最后添加)

include vhost/*.conf;                    (指定虚拟主机目录,并读取以.conf结尾的文件)

:wq退出保存

[[email protected] ~]# mkdir /usr/local/nginx/conf/vhost    (创建虚拟主机目录)

[[email protected] ~]# vim aaa.com.conf                (创建虚拟主机配置文件并添加以下内容:)

server

{

listen 80 default_server;                 (红色的字表示设置这个虚拟主机为默认虚拟主机)

server_name aaa.com;

index index.html index.htm index.php;

root /data/wwwroot/default;

}

[[email protected] vhost]# mkdir -p /data/wwwroot/default/     (创建虚拟主机的访问目录)

[[email protected] vhost]# echo "This is a default site." >/data/wwwroot/default/index.html      (编写虚拟主机主页)

[[email protected] vhost]# /usr/local/nginx/sbin/nginx -t          (检查配置文件语法错误)

[[email protected] vhost]# /usr/local/nginx/sbin/nginx -s reload        (重新加载配置文件)

[[email protected] vhost]# curl localhost                       (curl本机,发现到达nginx的虚拟主机主页)

This is a default site.

!!:还有一个需要注意的是,如果不加红色字体的字段,再找server时会根据文件名排序,比如:aaa.com.cnf和bbb.com.cnf,aaa肯定是在前,所以aaa.com.cnf是默认虚拟主机


三、Nginx用户认证

nginx用户认证

用到了之前httpd的htpasswd功能。

[[email protected] ~]# vim /usr/local/nginx/conf/vhost/test.com.conf             (创建一个虚拟主机)

server

{

listen 80;

server_name test.com;

index index.html index.htm index.php;

root /data/wwwroot/test.com;

location  /

{

auth_basic              "Auth";                   (定义用户认证的名字)

auth_basic_user_file   /usr/local/nginx/conf/htpasswd         (定义用户名密码文件)

}

}

因为要使用到httpd的htpasswd功能,则需要安装httpd,可以直接yum安装,直接敲htpasswd命令,

[[email protected] ~]# htpasswd -c /usr/local/nginx/conf/htpasswd lty (c是生成用户文件,若要添加则不需要,否则会覆盖原文件)

New password:

Re-type new password:

Adding password for user lty

检查语法错误,并且重新加载配置文件:(如果配置文件出现错误,reload不会使错误的配置文件生效)

[[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 test.com -I                   (不加用户发现401,需要用户认证)

HTTP/1.1 401 Unauthorized

Server: nginx/1.8.0

Date: Thu, 14 Dec 2017 04:15:02 GMT

Content-Type: text/html

Content-Length: 194

Connection: keep-alive

WWW-Authenticate: Basic realm="Auth"

[[email protected] ~]# curl -ulty:westos -x127.0.0.1:80 test.com          (-u指定用户和密码后,返回值)

test.com

1.需求;访问一个目录或者文件时,才需要用户认证。

实现:

[[email protected] ~]# vim /usr/local/nginx/conf/vhost/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目录)

{

auth_basic              "Auth";

auth_basic_user_file   /usr/local/nginx/conf/htpasswd;

}

}

检测语法错误并且重新加载配置文件:

[[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 test.com          (访问test.com时不需要密码也可以返回值)

test.com

[[email protected] ~]# curl -x127.0.0.1:80 test.com/admin         (访问test.com下的admin时,401需要用户认证)

<html>

<head><title>401 Authorization Required</title></head>

<body bgcolor="white">

<center><h1>401 Authorization Required</h1></center>

<hr><center>nginx/1.8.0</center>

</body>

</html>

需求,访问test.com下的1.php需要用户认证,

[[email protected] ~]# vim /usr/local/nginx/conf/vhost/test.com.conf

server

{

listen 80;

server_name test.com;

index index.html index.htm index.php;

root /data/wwwroot/test.com;

location  /admin/1.php                 (这里修改匹配到1.php)

{

auth_basic              "Auth";

auth_basic_user_file   /usr/local/nginx/conf/htpasswd;

检测语法错误并且重新加载配置文件:

[[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 test.com/admin/1.php           (不加用户密码访问发现401)

<html>

<head><title>401 Authorization Required</title></head>

<body bgcolor="white">

<center><h1>401 Authorization Required</h1></center>

<hr><center>nginx/1.8.0</center>

</body>

</html>

[[email protected] ~]# curl -ulty:westos -x127.0.0.1:80 test.com/admin/1.php  (加用户密码访问则正常返回)

touch file.php


四、nginx域名重定向

httpd配置文件里server_name后面不支持写多个域名,就算写了多个,也默认识别第一个

nginx的配置文件server_name后面则支持写多个域名,

[[email protected] ~]# vim /usr/local/nginx/conf/vhost/test.com.conf

server

{

listen 80;

server_name test.com test1.com test2.com;                   (server_name后跟多个域名)

index index.html index.htm index.php;

root /data/wwwroot/test.com;

if ($host != 'test.com' ) {                          (如果域名不是test.com)

rewrite  ^/(.*)$  http://test.com/$1  permanent; (rewrite到test.com,permanent301报错  redirect302报错)

检查语法错误并且重新加载配置文件:

[[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/admin/1.php -I           (访问test1时,提示301,跳转到test.comx下)

HTTP/1.1 301 Moved Permanently

Server: nginx/1.8.0

Date: Thu, 14 Dec 2017 05:03:32 GMT

Content-Type: text/html

Content-Length: 184

Connection: keep-alive

Location: http://test.com/admin/1.php

[[email protected] ~]# curl -x127.0.0.1:80 test2.com/admin/1.php -I          (访问test2时,提示301,跳转到test.comx下)

HTTP/1.1 301 Moved Permanently

Server: nginx/1.8.0

Date: Thu, 14 Dec 2017 05:03:38 GMT

Content-Type: text/html

Content-Length: 184

Connection: keep-alive

Location: http://test.com/admin/1.php


五、Nginx配置文件详解

http://www.ha97.com/5194.html

https://my.oschina.net/duxuefeng/blog/34880

原文地址:http://blog.51cto.com/13407306/2057083

时间: 2024-08-03 15:32:25

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

Nginx安装,默认虚拟主机以及认证和重定向

Nginx安装 1.首先下载安装包 [[email protected] src]# wget http://nginx.org/download/nginx-1.12.2.tar.gz --2018-03-14 00:46:57-- http://nginx.org/download/nginx-1.12.2.tar.gz 正在解析主机 nginx.org (nginx.org)... 206.251.255.63, 95.211.80.227, 2606:7100:1:69::3f, ...

nginx-设置默认虚拟主机、设置域名重定向、设置用户认证

Nginx默认虚拟主机 编辑nginx.comf vim /usr/local/nginx/conf/nginx.conf 删除server段 加入include vhost/*.conf: 代码预览 user nobody nobody; worker_processes 2; error_log /usr/local/nginx/logs/nginx_error.log crit; pid /usr/local/nginx/logs/nginx.pid; worker_rlimit_nofi

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 2

Linux学习总结(四十)lnmp之nginx安装 用户认证 域名重定向

1 nginx 介绍 Nginx官网 nginx.org,最新版1.13,最新稳定版1.12 Nginx应用场景:web服务.反向代理.负载均衡Nginx著名分支,淘宝基于Nginx开发的Tengine,使用上和Nginx一致,服务名,配置文件名都一样,和Nginx的最大区别在于Tenging增加了一些定制化模块,在安全限速方面表现突出,另外它支持对js,css合并Nginx核心+lua相关的组件和模块组成了一个支持lua的高性能web容器openresty 2 nginx 安装 cd /usr

Nginx 配置默认虚拟主机

为什么要配置默认的虚拟主机:http://www.cnblogs.com/pzk7788/p/7039496.html [[email protected] ~]# vim /usr/local/nginx/conf/vhosts/default.conf server { listen 80 default_server; server_name localhost; index index.html index.htm index.php; root /tmp/1233; deny all;

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 

12.6 Nginx安装;12.7 Nginx默认虚拟主机;12.8 Nginx用户认证;12.9

扩展: nginx.conf 配置详解 : http://www.ha97.com/5194.html http://my.oschina.net/duxuefeng/blog/34880 nginx rewrite四种flag : http://www.netingcn.com/nginx-rewrite-flag.html http://unixman.blog.51cto.com/10163040/1711943 12.6 Nginx安装 1. 进入下载安装包目录: [[email pro

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

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

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

Nginx安装 首先进入/usr/local/src目录.然后下载Nginx.wget http://nginx.org/download/nginx-1.12.1.tar.gz然后解压tar zxf nginx-1.12.1.tar.gz然后进入我们刚才解压好的目录进行编译cd nginx-1.12.1[[email protected] nginx-1.12.1]# ./configure --prefix=/usr/local/nginx这里没有加编译参数,我们可以根据实际情况,在后期编译