cd /usr/local/src
wget http://nginx.org/download/nginx-1.12.1.tar.gz =nginx下载地址(或者直接可以去官网下载)
tar zxf nginx-1.12.1.tar.gz = 解压下载包
cd nginx-1.12.1 = 进入解压好的目录
./configure --prefix=/usr/local/nginx =编译nginx (编译时可以根据需求添加需要的模块)
make && make install = 继续安装
vim /etc/init.d/nginx //复制如下内容(参考https://coding.net/u/aminglinux/p/aminglinux-book/git/blob/master/D15Z/etc_init.d_nginx?public=true ) = 编辑启动脚本
#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 = 开启开机服务自启动 on=开启 off=关闭
cd /usr/local/nginx/conf/; mv nginx.conf nginx.conf.bak = 进入nginx配置目录给原本的配置文件更改一个名字
vim nginx.conf //写入如下内容(参考https://coding.net/u/aminglinux/p/aminglinux-book/git/blob/master/D15Z/nginx.conf) = 写入自己的配置文件
#nginx配置文件
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_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
{
listen 80;
server_name localhost;
index index.html index.htm index.php;
root /usr/local/nginx/html;
location ~ \.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;
}
}
}
/usr/local/nginx/sbin/nginx -t = nginx查看自身语法是否有错误
/etc/init.d/nginx start = 启动nginx
netstat -lntp |grep 80 = 查看端口 nginx监听80端口
systemctl status nginx.service = 如果启动nginx 出问题可以使用命令来判断问题点在哪
vi /usr/local/nginx/html/1.php //加入如下内容 <?php echo "test php scripts."; ?> curl localhost/1.php
vim /usr/local/nginx/conf/nginx.conf //增加 = 更改原来的配置文件
include vhost/*.conf = 增加新定义的配置文件
mkdir /usr/local/nginx/conf/vhost =在conf目录下新创建一个vhost目录
cd !$; vim default.conf //加入如下内容 = vhost目录下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/ = 配置里面定义的网站根目录 (如果有那么就可以不用创建)
cd /data/wwwroot/default/ = 进入到创建的网站根目录随便写点东西测试使用
vim index.html =定义一个测试的配置文件 echo “This is a default site.”
echo “This is a default site.”>/data/wwwroot/default/index.html = 可以直接写入
/usr/local/nginx/sbin/nginx -t = 判断语法错误
/usr/local/nginx/sbin/nginx -s reload = 重新加载配置文件
curl localhost = 测试解析本机
curl -x127.0.0.1:80 123.com = 测试解析其他域名
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; =定义用户密码文件
}
}
yum install -y httpd = 工具需求安装
htpasswd -c /usr/local/nginx/conf/htpasswd aming =生成一个用户名和密码文件(如果还需要继续生成第二个用户和密码那么就不用加 -c )
cat 可以查看用生成的用户和密码
-t && -s reload //测试配置并重新加载
[root@aming-01 vhost]# /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@aming-01 vhost]# /usr/local/nginx/sbin/nginx -s reload = 重新加载配置文件
mkdir /data/wwwroot/test.com = 创建一个测试配置文件目录
echo “test.com”>/data/wwwroot/test.com/index.html = 创建的测试配置文件
curl -x127.0.0.1:80 test.com -I//状态码为401说明需要验证 = 测试 出现401说明需要认证用户
curl -uaming:passwd 访问状态码变为200 = 认证用户后测试状态为200
[root@aming-01 vhost]# curl -uaming:rabbit -x127.0.0.1:80 test.com
“test.com”
编辑windows的hosts文件,然后在浏览器中访问test.com会有输入用户、密码的弹窗针对目录的用户认证
location /admin/
{
auth_basic "Auth";
auth_basic_user_file /usr/local/nginx/conf/htpasswd;
}
更改test.com.conf
server
{
listen 80;
server_name test.com test1.com test2.com;
index index.html index.htm index.php;
root /data/wwwroot/test.com;
if ($host != 'test.com' ) {
rewrite ^/(.*)$ http://test.com/$1 permanent;
}
}
server_name后面支持写多个域名,这里要和httpd的做一个对比
permanent为永久重定向,状态码为301,如果写redirect则为302
nginx.conf 配置详解 http://www.ha97.com/5194.htmlhttp://my.oschina.net/duxuefeng/blog/34880 nginx rewrite四种flag
http://www.netingcn.com/nginx-rewrite-flag.htmlhttp://unixman.blog.51cto.com/10163040/1711943