Nginx 笔记与总结(3)配置虚拟主机
Nginx 重启的另外一种方式,相当于 kill -HUP `cat /usr/local/nginx/logs/nginx.pid`:
/usr/local/nginx/sbin/nginx -s reload
停止 Nginx 的另外一种方式:
/usr/local/nginx/sbin/nginx -s stop
重读日志文件的另一种方式,相当于 kill -USR1 `cat /usr/local/nginx/logs/nginx.pid`:
/usr/local/nginx/sbin/nginx -s reopen
测试配置文件是否正确:
/usr/local/nginx/sbin/nginx -t
如果配置正确,则会提示 successful:
虚拟主机的管理
vim /usr/local/nginx/conf/nginx.conf
全局区:
worker_processes 1 表示有 1 个工作的子进程,可以进行修改,但是过大没有意义,因为需要占用更多的 CPU 资源,一般设置为 CPU 数 * 核数
events 区:
一般配置 Nginx 进程与连接的特性。worker_connections 1024 表示 1 个子进程(worker)最多允许 1024 个连接。
http 段:
1 http { 2 include mime.types; 3 default_type application/octet-stream; 4 5 #log_format main '$remote_addr - $remote_user [$time_local] "$request" ' 6 # '$status $body_bytes_sent "$http_referer" ' 7 # '"$http_user_agent" "$http_x_forwarded_for"'; 8 9 #access_log logs/access.log main; 10 11 sendfile on; 12 #tcp_nopush on; 13 14 #keepalive_timeout 0; 15 keepalive_timeout 65; 16 17 #gzip on; 18 19 server { 20 listen 80; 21 server_name localhost; 22 23 #charset koi8-r; 24 25 #access_log logs/host.access.log main; 26 27 location / { 28 root html; 29 index index.html index.htm; 30 } 31 32 #error_page 404 /404.html; 33 34 # redirect server error pages to the static page /50x.html 35 # 36 error_page 500 502 503 504 /50x.html; 37 location = /50x.html { 38 root html; 39 } 40 41 # proxy the PHP scripts to Apache listening on 127.0.0.1:80 42 # 43 #location ~ \.php$ { 44 # proxy_pass http://127.0.0.1; 45 #} 46 47 # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 48 # 49 #location ~ \.php$ { 50 # root html; 51 # fastcgi_pass 127.0.0.1:9000; 52 # fastcgi_index index.php; 53 # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; 54 # include fastcgi_params; 55 #} 56 57 # deny access to .htaccess files, if Apache's document root 58 # concurs with nginx's one 59 # 60 #location ~ /\.ht { 61 # deny all; 62 #} 63 } 64 65 66 # another virtual host using mix of IP-, name-, and port-based configuration 67 # 68 #server { 69 # listen 8000; 70 # listen somename:8080; 71 # server_name somename alias another.alias; 72 73 # location / { 74 # root html; 75 # index index.html index.htm; 76 # } 77 #} 78 79 80 # HTTPS server 81 # 82 #server { 83 # listen 443 ssl; 84 # server_name localhost; 85 86 # ssl_certificate cert.pem; 87 # ssl_certificate_key cert.key; 88 89 # ssl_session_cache shared:SSL:1m; 90 # ssl_session_timeout 5m; 91 92 # ssl_ciphers HIGH:!aNULL:!MD5; 93 # ssl_prefer_server_ciphers on; 94 95 # location / { 96 # root html; 97 # index index.html index.htm; 98 # } 99 #} 100 101 }
View Code
配置 http 服务器的主要的段。
http 段中的 server 段:
server {listen 80;server_name localhost;#charset koi8-r;#access_log logs/host.access.log main;location / {root html;index index.html index.htm;}#error_page 404 /404.html;# redirect server error pages to the static page /50x.html#error_page 500 502 503 504 /50x.html;location = /50x.html {root html;}# proxy the PHP scripts to Apache listening on 127.0.0.1:80##location ~ \.php$ {# proxy_pass http://127.0.0.1;#}# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000##location ~ \.php$ {# root html;# fastcgi_pass 127.0.0.1:9000;# fastcgi_index index.php;# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;# include fastcgi_params;#}# deny access to .htaccess files, if Apache's document root# concurs with nginx's one##location ~ /\.ht {# deny all;#}}
server 就是虚拟主机。
【例1】基于域名的虚拟主机
添加 server 段:
server{listen 80;server_name dee.com;location /{root dee.com;index index.html;}}
保存退出;
location 可以使用绝对路径,也可以使用相对路径,相对路径是相对于 nginx 的根目录:/usr/local/nginx
在 /usr/local/nginx 目录下创建目录 dee.com,在其下创建 index.html:
[root@localhost ~]# cd /usr/local/nginx/ [root@localhost nginx]# mkdir dee.com [root@localhost nginx]# vim dee.com/index.html
index.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>dee.com</title>
<body>
Welcome to dee.com
</body>
</head>
</html>
重新读取配置文件:
/usr/local/nginx/sbin/nginx -s reload
配置 hosts:
192.168.254.100 dee.com
访问 dee.com:
【例2】基于 ip 的虚拟主机
编辑配置文件:
vim /usr/local/nginx/conf/nginx.conf
添加 server 段:
server{listen 80;server_name 192.168.254.100;location /{root ip;index index.html;}}
在 /usr/local/nginx 目录下新建 ip 目录:
[root@localhost nginx]# mkdir ip [root@localhost nginx]# vim ip/index.html
index.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>dee.com</title>
<body>
ip test
</body>
</head>
</html>
访问 http://192.168.254.100/
【例3】基于端口的虚拟主机
编辑配置文件(vim 查看行号 :set nu):
vim /usr/local/nginx/conf/nginx.conf
添加 server 段:
server{listen 2022;server_name dee.com;location /{root /var/www;index index.html;}}
这时配置 location 使用绝对路径;
在 /var 下新建 www 目录,在其下新建 index.html 文件:
[root@localhost nginx]# mkdir /var/www [root@localhost nginx]# vim /var/www/index.html <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>dee.com</title> <body> Welcome to dee.com's admin panel </body> </head> </html>
重新读取配置文件:
/usr/local/nginx/sbin/nginx -s reload
访问 http://dee.com:2022/
或者访问 http://192.168.254.100:2022/
附:如果需要启用目录浏览,只需要在 Server 段中加入 autoindex on;
转载于:https://www.cnblogs.com/dee0912/p/4671764.html
Nginx 笔记与总结(3)配置虚拟主机相关推荐
- Nginx基于IP,端口,域名配置虚拟主机
Nginx(发音同 engine x)是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like 协议下发行.其特点是占有内存少,并发能力强,事实 ...
- nginx 配置虚拟主机
文章转载自:http://www.ttlsa.com/html/1571.html 上篇说道我们的nginx是安装在/usr/local/nginx/ cd conf 我们现在把所有的虚拟主机放在一个 ...
- Nginx(二)配置虚拟主机
1.配置虚拟主机 虚拟主机,也叫"⽹站空间",就是把⼀台运⾏在互联⽹上的物理服务器划分成多个"虚拟"服务器.虚拟主 机技术极⼤的促进了⽹络技术的应⽤和普及.同时 ...
- Nginx配置虚拟主机三种方式
Nginx 配置虚拟主机,总共有三种方式,基于多IP,基于多端口,基于多域名,其中基于多域名是企业中最常用的一种方式,基于多端口的方式,主要用于本机配置项目. nginx参数详解 基于多IP的方式 [ ...
- nginx配置虚拟主机vhost
需求 项目需要两个域名使用同一个服务器,配置多个项目文件. 所谓虚拟主机,是说通过几个不同的url地址,都能到达nginx环境,只不过针对不同的url,处理的逻辑不同.nginx支持虚拟主机,但是浏览 ...
- 正向代理和反向代理的区别Nginx配置虚拟主机流程(后续更新)
目录 目标 安装Nginx 配置虚拟主机 准备 方法一 方法二(推荐) 验证虚拟主机 正向代理和反向代理的区别 区别&案例 正向代理和反向代理流程 目标 熟练在Linux安装单机Nginx: ...
- Nginx1.0.9配置虚拟主机
Nginx1.0.9配置虚拟主机<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office&quo ...
- Linux 配置LNMP服务器 并配置虚拟主机
2019独角兽企业重金招聘Python工程师标准>>> 一.停止甚至删除系统上现有的web服务器软件 为了防止出现意外情况,建议先卸载现有的所有web服务器资源,如apache.my ...
- windows下apache配置虚拟主机的两个方法
windows下apache配置虚拟主机方法一: 对httpd.conf进行设置: 1.注释以下三行 #ServerAdmin #ServerName #DocumentRoot 2.去掉mod_pr ...
- 解决Apache配置虚拟主机时出现403错误的问题
1.用文本编辑器打开Apache中的httpd.conf,搜索httpd-vhosts.conf,找到"#Include conf/extra/httpd-vhosts.conf" ...
最新文章
- java i18n 转换,Java的国际化支持(I18N问题)
- 让尸体说话-法医密档
- 【 FPGA 】FIR 滤波器的架构
- python类class基础
- 【高并发】ThreadLocal、InheritableThreadLocal
- iPhone在scrollview中双击定点放大的代码
- Django3与Vue3前后端分离搭建
- Jenkins通过FTP上传站点太多文件导致太慢且不稳定,切换为压包上传再解压的思路(asp.net)...
- java编程软件安装
- 干货,AES破解路程-生意参谋举例
- windows安装ffmpeg,yasm,opencv
- 使用ado访问excel中的数据
- 生成缩略图 java_java实现生成缩略图
- 【已解决】iphone和mac的备忘录、提醒事项、日历等无法同步。MacBook点击“更新Apple ID 设置”无反应。
- 常见的数据校验方式(奇偶,累加,CRC校验)
- PostgreSQL 创建分区表
- Blender入门级使用
- mac book air 安装win10
- PHPCMS V9 全站调用会员信息代码
- mac osx下载安装java运行环境
热门文章
- python计算汽车的平均油耗_用python对汽车油耗进行数据分析
- 金融项目app服务器配置,云在金融的应用
- c# 口口乱码_c# 乱码解决方法
- matlab多项式加法运算,matlab多项式运算与代数方程求解解析.ppt
- C++ 内存基本构件new [] /delete []的意义、内存泄漏原因、VC下cookie的基本布局
- Python字符串格式:%vs.format
- Java布尔类toString()方法及示例
- 十、非规则组织分析及其数学模型——锯齿形斜纹组织
- 用筛选法求100之内的素数
- thinkphp mysql日志_MySQL的日志基础知识及基本操作学习教程