您的位置:首页 > 运维架构 > Nginx

lnmp-nginx实现虚拟主机

2014-04-20 22:27 274 查看
##############################
动态页面由php-fastcgi管理
静态页面由nginx自己处理
#############################
nginx虚拟主机1
l nginx并发性[root@server79 conf]# vim nginx.conf
worker_processes 3 ;
[root@server79 conf]# ps axu //查看到nginx的worker进程开启了三个
nginx 4086 0.0 0.4 45556 1988 ? S 16:48 0:00 nginx: worker p
nginx 4087 0.0 0.4 45556 1988 ? S 16:48 0:00 nginx: worker p
nginx 4088 0.0 0.4 45556 1988 ? S 16:48 0:00 nginx: worker p

状态检查:
location / {
root html;
index index.php index.html index.htm;
}
location /status{
stub_status on;
access_log off;
}

检测配置是否正确:
[root@server79 conf]# nginx -t
[root@server79 conf]# nginx -s reload

测试:
浏览器中输入:
http://192.168.0.179/index.html



刷新以下url的页面,requests会增加 http://192.168.0.179/status




l 虚拟主机:[root@server79 conf]# vim nginx.conf
# HTTPS server
#
server {
listen 443;
server_name server79.example.com;

ssl on;
ssl_certificate cert.pem;
ssl_certificate_key cert.pem;

ssl_session_timeout 5m;

ssl_protocols SSLv2 SSLv3 TLSv1;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;

location / {
root html;
index index.html index.htm;
}
}

制作加密证书:
[root@server79 conf]# cd /etc/pki/tls/certs/
[root@server79 certs]# make cert.pem
umask 77 ; \
PEM1=`/bin/mktemp /tmp/openssl.XXXXXX` ; \
PEM2=`/bin/mktemp /tmp/openssl.XXXXXX` ; \
/usr/bin/openssl req -utf8 -newkey rsa:2048 -keyout $PEM1 -nodes -x509 -days 365 -out $PEM2 -set_serial 0 ; \
cat $PEM1 > cert.pem ; \
echo "" >> cert.pem ; \
cat $PEM2 >> cert.pem ; \
rm -f $PEM1 $PEM2
Generating a 2048 bit RSA private key
....+++
............................+++
writing new private key to '/tmp/openssl.DLz5xj'
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:SHANNXI
Locality Name (eg, city) [Default City]:XI'AN
Organization Name (eg, company) [Default Company Ltd]:LINUX
Organizational Unit Name (eg, section) []:WESTOS
Common Name (eg, your name or your server's hostname) []:server79.example.com
Email Address []:root@server79.example.com

检测配置并重新加载:
[root@server79 conf]# nginx -t
[root@server79 conf]# nginx -s reload

测试:
浏览器https://server79.example.com



看到以下时成功:



nginx虚拟主机2:
修改nginx的主配置文件:
[root@server79 conf]# vim nginx.conf
server {
listen 80;
server_name www.westos.org;
access_log logs/westos.org.access.log main;
location / {
index index.html;
root /usr/local/lnmp/nginx/virtualhost/westos.org;
}
}
server {
listen 80;
server_name www.linux.org;
access_log logs/linux.org.access.log main; //需打开日志格式
location / {
index index.html;
root /usr/local/lnmp/nginx/virtualhost/linux.org;
}
}

打开日志格式:

log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';

检测有无语法错误:
[root@server79 conf]# nginx -t
nginx: the configuration file /usr/local/lnmp/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/lnmp/nginx/conf/nginx.conf test is successful

新建虚拟主机目录,编辑测试文件:
[root@server79 nginx]# mkdir virtualhost
[root@server79 nginx]# cd virtualhost/
[root@server79 virtualhost]# ls
[root@server79 virtualhost]# mkdir westos.org
[root@server79 virtualhost]# mkdir linux.org

[root@server79 westos.org]# echo www.westos.org cuncun > index.html
[root@server79 westos.org]# cat index.html
www.westos.org cuncun
[root@server79 westos.org]# cd ..
[root@server79 virtualhost]# cd linux.org/
[root@server79 linux.org]# ls
[root@server79 linux.org]# echo www.linux.org cuncun > index.html
[root@server79 linux.org]# cat index.html
www.linux.org cuncun

重新加载nginx:
[root@server79 conf]# nginx -s reload

测试:
在浏览器的宿主机上做解析:
vi /etc/hosts
192.168.0.179 www.westos.org www.linux.org

浏览器中输入:
http://www.westos.org



htpp://www.linux.org



具体图片请查看附件
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: