Nginx的安裝,預設虛擬主機,使用者認證,域名重定向
阿新 • • 發佈:2018-11-27
Nginx 安裝
下載安裝包,解壓編譯安裝
[[email protected] etc]# cd /usr/local/src/
[[email protected] src]# wget http://nginx.org/download/nginx-1.14.1.tar.gz
[[email protected] src]# tar -zxvf nginx-1.14.1.tar.gz
[[email protected] nginx-1.14.1]# ./configure --prefix=/usr/local/nginx
[[email protected] nginx-1.14.1]# make
[[email protected] nginx-1.14.1]# make install
編寫啟動指令碼,啟動
啟動指令碼內容:
#!/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] nginx-1.14.1]# vim /etc/init.d/nginx
[[email protected] nginx-1.14.1]# chmod 755 /etc/init.d/nginx
[[email protected] nginx-1.14.1]# chkconfig --add nginx
[[email protected] nginx-1.14.1]# chkconfig nginx on
[[email protected] nginx-1.14.1]# ls /usr/local/nginx/
conf html logs sbin
[ [email protected] nginx-1.14.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
新建配置檔案,內容如下
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_pass 127.0.0.1:9000; # tcp方式
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
}
}
}
啟動
[[email protected] conf]# mv nginx.conf nginx.conf.orignal
[[email protected] conf]# vim nginx.conf # 新增上面的nginx內容
[[email protected] 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
[[email protected] conf]# /etc/init.d/nginx start
Starting nginx (via systemctl): [ OK ]
測試訪問 [root@test-a conf]# curl localhost
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
[[email protected] conf]# cd ../html/
[[email protected] html]# vim index.php
[[email protected] html]# cat index.php
<?php
echo "Hello,nginx";
[[email protected] html]# curl localhost/index.php
Hello,nginx[[email protected] html]#
Nginx 預設虛擬主機
先把nginx.conf中的server部分內容去掉,換成include 虛擬主機配置,參考如下
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;
include "vhost/*.conf";
}
建立虛擬主機配置目錄及檔案
[[email protected] conf]# mkdir vhost
[[email protected] conf]# cd vhost/
[[email protected] vhost]# vim test.com.conf # 新增如下內容
server
{
listen 80 default_server;
server_name test.com;
index index.html index.htm index.php;
root /data/wwwroot/default;
}
建立站點目錄,新增檔案,測試
[[email protected] conf]# mkdir -p /data/wwwroot/default
[[email protected] conf]# cd /data/wwwroot/default/
[[email protected] default]# vim index.html
[[email protected] default]# cat index.html
This is nginx default site.
[[email protected] 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
[[email protected] default]# /usr/local/nginx/sbin/nginx -s reload
[[email protected] vhost]# curl localhost
This is nginx default site.
Nginx使用者認證
新增虛擬站點配置,生成認證賬戶
[[email protected] vhost]# vim abc.com.conf
[[email protected] vhost]# cat abc.com.conf # 新增如下內容
server
{
listen 80;
server_name abc.com;
index index.html index.htm index.php;
root /data/wwwroot/abc.com;
location /
{
auth_basic "Auth";
auth_basic_user_file /usr/local/nginx/conf/vhost/nginx-htpasswd;
}
}
# 如果沒有htpasswd, 可以yum install -y httpd安裝即可
[[email protected] vhost]# /usr/local/apache2.4/bin/htpasswd -c nginx-htpasswd test
New password:
Re-type new password:
Adding password for user test
[[email protected] vhost]# ls
abc.com.conf nginx-htpasswd test.com.conf
[[email protected] vhost]# cat nginx-htpasswd
test:$apr1$p3836SLL$Atra.KlKSpLnEWb8lKDNh1
[[email protected] vhost]# /usr/local/apache2.4/bin/htpasswd nginx-htpasswd test1
New password:
Re-type new password:
Adding password for user test1
建立站點目錄及檔案,重新載入伺服器配置
[[email protected] vhost]# mkdir /data/wwwroot/abc.com
[[email protected] vhost]# cd /data/wwwroot/abc.com
[[email protected] abc.com]# vim index.html
[[email protected] abc.com]# cat index.html
This is abc.com site.
[[email protected] 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
[[email protected] vhost]# /usr/local/nginx/sbin/nginx -s reload
訪問測試
[[email protected] abc.com]# curl -x127.0.0.1:80 abc.com
<html>
<head><title>401 Authorization Required</title></head>
<body bgcolor="white">
<center><h1>401 Authorization Required</h1></center>
<hr><center>nginx/1.14.1</center>
</body>
</html>
[[email protected] abc.com]# curl -utest1:test1 -x127.0.0.1:80 abc.com
This is abc.com site.
如果訪問返回403狀態碼,有幾種可能:
- 使用者名稱密碼不正確
- 祕鑰檔案配置不正確
- 沒有配置索引頁,即index index.html
- 站點目錄沒有對應的索引頁
- 索引頁及相應的目錄許可權不正確
- nginx啟動的使用者和站點使用者不一致
Nginx 域名重定向
配置,重新載入配置
[[email protected] vhost]# vim abc.com.conf
[[email protected] vhost]# cat abc.com.conf
server
{
listen 80;
server_name abc.com ab.com a.com; # 這裡和apache的ServerName配置有點差別,apache不能配置多個,配置了也只能識別第一個,只能通過ServerAlias來配置。
index index.html index.htm index.php;
root /data/wwwroot/abc.com;
if ($host != 'abc.com'){
rewrite ^/(.*)$ http://abc.com/$1 permanent; # permanent 301, redirect 302
}
}
[[email protected] 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] nginx]# ./sbin/nginx -s reload
訪問測試
[[email protected] nginx]# curl -x127.0.0.1:80 ab.com -I
HTTP/1.1 301 Moved Permanently
Server: nginx/1.14.1
Date: Mon, 26 Nov 2018 22:59:41 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: http://abc.com/
[[email protected] nginx]# curl -x127.0.0.1:80 a.com -I
HTTP/1.1 301 Moved Permanently
Server: nginx/1.14.1
Date: Mon, 26 Nov 2018 22:59:55 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: http://abc.com/