学习linux第四十九天

nginx安装

[root@hanlin nginx-1.12.1]# wget http://nginx.org/download/nginx-1.12.1.tar.gz

[root@hanlin nginx-1.12.1]# tar -zxvf nginx-1.12.1.tar.gz 

[root@hanlin src]# cd nginx-1.12.1/
[root@hanlin nginx-1.12.1]# ./configure --prefix=/usr/local/nginx
[root@hanlin nginx-1.12.1]# make && make install
[root@hanlin nginx-1.12.1]# ls /usr/local/nginx/
conf(配置文件)  html(样例)  logs(日志)  sbin(启动文件)
[root@hanlin nginx-1.12.1]# /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@hanlin nginx-1.12.1]# 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
[root@hanlin nginx-1.12.1]# chmod 755 !$
chmod 755 /etc/init.d/nginx

 

[root@hanlin conf]# cd /usr/local/nginx/conf

[root@hanlin conf]# mv conf/nginx.conf conf/nginx.conf.bak

[root@hanlin conf]# vim nginx.conf

user nobody 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; (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 (相当于是apache的默认虚拟主机配置)
{
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; (指定监听端口也可以直接输入ip加端口)
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;

}
}

 

[root@hanlin local]# /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@hanlin local]# systemctl nginx start
Unknown operation 'nginx'.
[root@hanlin local]# /etc/init.d/nginx start (启动nginx)
Starting nginx (via systemctl): [ 确定 ]
[root@hanlin local]# ps -aux |grep nginx
root 8938 0.0 0.0 20496 620 ? Ss 17:51 0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nobody 8939 0.0 0.1 25024 3496 ? S 17:51 0:00 nginx: worker process
nobody 8940 0.0 0.1 25024 3232 ? S 17:51 0:00 nginx: worker process
root 8950 0.0 0.0 112676 984 pts/0 S+ 17:52 0:00 grep --color=auto nginx

 

[root@hanlin local]# vim /usr/local/nginx/html/1.php

<?php
echo "nginx"
[root@hanlin local]# curl localhost/1.php 
nginx[root@hanlin local]# (解析成功)

 

 

nginx默认虚拟主机配置

[root@hanlin local]# vim /usr/local/nginx/conf/nginx.conf (删除掉server相关配置,最后加一行)
 

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;
include vhost/*.conf; (在conf目录下加上vhost的子目录,需要自己创建)
}
[root@hanlin conf]# pwd
/usr/local/nginx/conf
[root@hanlin conf]# mkdir vhost
[root@hanlin conf]# cd vhost/
[root@hanlin vhost]# vim xy.conf

 

server
{
listen 80 default_server; // 有这个标记的就是默认虚拟主机
server_name aaa.com;
index index.html index.htm index.php;
root /data/wwwroot/default;
}

[root@hanlin vhost]# mkdir -p /data/wwwroot/default
[root@hanlin vhost]# cd !$
cd /data/wwwroot/default
[root@hanlin default]# vim index.html
this is the default site.

 

[root@hanlin default]# /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@hanlin default]# /etc/init.d/nginx restart (重启)
Restarting nginx (via systemctl): [ 确定 ]
[root@hanlin default]# /usr/local/nginx/sbin/nginx -s reload (或者重新加载)
[root@hanlin default]# 
[root@hanlin default]# curl localhost
this is the default site.
[root@hanlin default]# curl -x127.0.0.1:80 123.com (因为只是默认虚拟主机,创建的conf当中系统会一个个往下认,第一个就是默认虚拟主机,或者在配置文件里指定default _server)
this is the default site.
[root@hanlin conf]# vim vhost/xy.conf 

 

server
{
listen 80 default_server;
server_name aaa.com;
index index.html index.htm index.php;
root /data/wwwroot/default;
}

 

 

 

[root@hanlin ~]# systemctl start php-fpm
[root@hanlin ~]# service start nginx
The service command supports only basic LSB actions (start, stop, restart, try-restart, reload, force-reload, status). For other actions, please try to use systemctl.
[root@hanlin ~]# /etc/init.d/nginx start
Starting nginx (via systemctl): [ 确定 ]
[root@hanlin ~]# systemctl start mysqld
[root@hanlin ~]# cd /usr/local/nginx/conf/vhost/
[root@hanlin vhost]# vim 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;

}

}

[root@hanlin vhost]# /usr/local/apache2.4.34/bin/htpasswd -c /usr/local/nginx/conf/htpasswd xy (再创建第二个账户时就不需要-c了不然会覆盖掉原来的文件)
New password: 
Re-type new password: 
Adding password for user xy
[root@hanlin vhost]# cat /usr/local/nginx/conf/htpasswd
xy:$apr1$uu4saZT8$0SLi1XP24Gjh37uKBPJpK1

[root@hanlin 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@hanlin vhost]# /usr/local/nginx/sbin/nginx -s reload
[root@hanlin vhost]# curl -x127.0.0.1:80 test.com
<html>
<head><title>401 Authorization Required</title></head>
<body bgcolor="white">
<center><h1>401 Authorization Required</h1></center>
<hr><center>nginx/1.12.1</center>
</body>
</html>
[root@hanlin vhost]# curl -x127.0.0.1:80 -u xy:abcd-1234 test.com
<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.12.1</center>
</body>
</html>
[root@hanlin vhost]# mkdir /data/wwwroot/test.com
[root@hanlin vhost]# echo "wo shi zhongguoren" >>/data/wwwroot/test.com/index.html
[root@hanlin vhost]# !curl
curl -x127.0.0.1:80 -u xy:abcd-1234 test.com
wo shi zhongguoren


针对具体目录做限制


location  /admin/

[root@hanlin vhost]# mkdir /data/wwwroot/test.com/admin
[root@hanlin admin]# echo "444444444444444" > !$
echo "444444444444444" > index.html
[root@hanlin admin]# cat !$
cat index.html
444444444444444
[root@hanlin admin]# curl -x127.0.0.1:80 test.com/admin/index.html (不指定用户名和密码就会报错401)
<html>
<head><title>401 Authorization Required</title></head>
<body bgcolor="white">
<center><h1>401 Authorization Required</h1></center>
<hr><center>nginx/1.12.1</center>
</body>
</html>
针对具体文件类型做用户认证

 

 

server
{
listen 80;
server_name test.com;
index index.html index.htm index.php;
root /data/wwwroot/test.com;

location ~admin.php
{
auth_basic "Auth";
auth_basic_user_file /usr/local/nginx/conf/htpasswd;
}
}
[root@hanlin admin]# /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@hanlin admin]# /usr/local/nginx/sbin/nginx -s reload
[root@hanlin admin]# curl -x127.0.0.1:80 test.com/admin/index.html
444444444444444
[root@hanlin admin]# curl -x127.0.0.1:80 test.com/admin.php (其他访问都正常,只有您访问admin.php才会报401的错)
<html>
<head><title>401 Authorization Required</title></head>
<body bgcolor="white">
<center><h1>401 Authorization Required</h1></center>
<hr><center>nginx/1.12.1</center>
</body>
</html>

 

nginx域名重定向

 

[root@hanlin admin]# !vim
vim /usr/local/nginx/conf/vhost/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;
}


location /
{
auth_basic "Auth";
auth_basic_user_file /usr/local/nginx/conf/htpasswd;
}
}
 

 

[root@hanlin admin]# curl -x127.0.0.1:80 test2.com/admin/index.html -I ()
HTTP/1.1 301 Moved Permanently
Server: nginx/1.12.1
Date: Sun, 25 Nov 2018 08:28:32 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: http://test.com/admin/index.html (重定向成功,如果不是记录的站点就回去访问默认虚拟主机,xy.conf里面记录的xy.com)
[root@hanlin wwwroot]# cd /usr/local/nginx/conf/vhost/

[root@hanlin vhost]# ls

test.com.conf xy.conf
[root@hanlin vhost]# vim xy.conf 
server

{
listen 80 default_server;
server_name aaa.com;
index index.html index.htm index.php;
root /data/wwwroot/default;
}

 

 

 

 

转载于:https://my.oschina.net/u/3867255/blog/2961485

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值