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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/397781.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

计算机如何查找目标,如何使用命令行查找计算机地理位置? | MOS86

有多种方法可以从IP地址中了解计算机的位置&#xff0c;但如果您决定使用命令行查找信息&#xff0c;那么您如何处理&#xff1f;今天今天的问题Screenshot由Paul Fenwick(Flickr)提供。问题SuperUser阅读器AlikElzin-kilaka想知道如何找到一台电脑首先&#xff0c;AlikElzin-k…

Nmap命令的常用实例

一、Nmap简介 nmap是一个网络连接端扫描软件&#xff0c;用来扫描网上电脑开放的网络连接端。确定哪些服务运行在哪些连接端&#xff0c;并且推断计算机运行哪个操作系统&#xff08;这是亦称 fingerprinting&#xff09;。它是网络管理员必用的软件之一&#xff0c;以及用以评…

mysql sqlexception_c-很奇怪-mysql的sql :: SQLException未被其类型捕...

我正在使用带有此(稍微简化)代码的mysql c连接器.try{statement->setString(1, word);statement->executeUpdate();}catch( sql::SQLException& e ){// I dont get herereturn sqlerrno_to_error_code( e.getErrorCode() );}catch( std::exception& e ){// I do …

Linux Kernel系列 - 黄牛X内核代码凝视

Hanks.Wang - 专注于操作系统与移动安全研究。Linux-Kernel/SELinux/SEAndroid/TrustZone/Encription/MDM Mail - byhankswanggmail.com 牛X的内核代码凝视 大牛的代码质量高稳定性好&#xff0c;并且逻辑清晰易读性比較强&#xff0c;今天看到Linux Kernel红黑树的代码时&a…

电子商务计算机网络安全技术教案,网络安全技术教案.pdf

名师精编 优秀教案《网络安全技术》教案2011-2012 第 2 学期适用班级&#xff1a; 2010 级计算机网络技术专业编写&#xff1a;徐英武名师精编 优秀教案第 1~2 课时周次 &#xff11; 日期 2012 年 02 月 14 日 授课班级 2010 级计算机网络技术课题 网络安全概述 课 型 理论课教…

技术文章汇总

点击以下链接&#xff0c;可以查看相关技术文章&#xff1a;包括APP软件开发、手机软件开发、嵌入式开发、Java和C/C编程&#xff0c;同时涉及原型设计、效果图设计、切图、网络、多媒体、加密、字符编码、通信原理、测试和项目管理等各方面的知识。APP开发实战手机开发实战技术…

咋样查mysql的url_eclipse用jdbc连接mysql数据库时,url是填什么?怎样找出地址?

展开全部 jdbc连接mysql数据62616964757a686964616fe78988e69d8331333337623535库的url为: jdbc:mysql://主机名或IP抵制:端口号/数据库名?useUnicode=true&characterEncoding=UTF-8 jdbc连接其他数据库的连接字符串写法为:1、Oracle8/8i/9i数据库(thin模式) Class.for…

HP服务器ile进系统,HP GEN10服务器UEFI安装Windows Sverver 2012 R2教程

1.操作系统&#xff1a;Windows Server 2012 R2 VL with Update (x64) – DVD (Chinese-Simplified)&#xff0c;MSDN下载地址&#xff1a;ed2k://|file|cn_windows_server_2012_r2_vl_with_update_x64_dvd_6052729.iso|5545527296|BD499EBCABF406AB82293DD8A5803493|/2.镜像写…

[程序设计语言] 堆和栈的全面总结

操作系统堆栈&#xff1a; 分配由编译器自己主动和自己主动释放。对应于堆栈的函数。参数存储功能值、函数调用结束后完成值和局部变量的函数体内。段内存空间。其操作和组织方式与数据结构中的栈十分相似。栈是为了运行线程留出的内存空间。当调用函数时创建栈。当函数运行完毕…

py文件的操作

文件操作基本流程。 计算机系统分为&#xff1a;计算机硬件&#xff0c;操作系统&#xff0c;应用程序三部分。 我们用python或其他语言编写的应用程序若想要把数据永久保存下来&#xff0c;必须要保存于硬盘中&#xff0c;这就涉及到应用程序要操作硬件&#xff0c;众所周知&a…

CentOS系统启动流程你懂否

一、Linux内核的组成相关概念&#xff1a;Linux系统的组成部分&#xff1a;内核根文件系统内核&#xff1a;进程管理、内存管理、网络协议栈、文件系统、驱动程序。IPC(Inter-Process Communication进程间通信):就是指多个进程之间相互通信&#xff0c;交换信息的方法。Linux I…

怎样用css设置图片下的投影,css – 做这种投影的最佳方法是什么?

如果您更喜欢使用CSS来创建该类型的阴影,则可以将CSS3用作seen here!CSS/* Lifted corners */.lifted {-moz-border-radius:4px;border-radius:4px;}.lifted:before,.lifted:after {bottom:15px;left:10px;width:50%;height:20%;max-width:300px;-webkit-Box-shadow:0 15px 10p…

mysql 排版 指令_Mysql语句排版

SQL 高效排版指北统一 SQL 排版的相关用法&#xff0c;极大提高编写和维护 SQL 的效率。注: column 选取的字段&#xff1b;table 选取的表名语句结构错误SELECT column1 FROM table1 ORDER BY column1正确SELECTcolumn1FROMtable1ORDER BYcolumn1解析SQL 语句在内部执行时会…

Linux命令学习手册-tr命令 2015-07-26 20:35 9人阅读 评论(0) 收藏...

tr [OPTION]... SET1 [SET2] [功能] 转换或者删除字符。 [描述] tr指令从标准输入设备读取数据&#xff0c;经过字符串转译后&#xff0c;输出到标准输出设备。 通过使用 tr&#xff0c;您可以非常容易地实现 sed 的许多最基本功能。您可以将 tr 看作为 sed 的&#xff08…

css商品,商品标签例子——CSS3 transform 属性

积累很重要。从此开始记录前端生涯的点滴....div{width:150px;height:30px;background-color:#f83944;/* Rotate div */transform:rotate(-40deg);-ms-transform:rotate(-40deg); /* Internet Explorer */-moz-transform:rotate(-40deg); /* Firefox */-webkit-transform:rotat…

The literal of int xxxxx is out of range

有时候我们定义了long型的变量&#xff0c;当我们给该变量赋值过长的整数时&#xff0c;系统依然会提示长度超过范围&#xff0c;解决办法如下&#xff1a; long timeShow 1437565243495L; 我们需要在整形变量的后面加上“L”&#xff0c;便可以避免系统报错。转载于:https://…

debian 访问 windows 共享_【续】windows环境redis未授权利用方式梳理

01Redis未授权产生原因1.redis绑定在0.0.0.0:6379默认端口&#xff0c;直接暴露在公网&#xff0c;无防火墙进行来源信任防护。2.没有设置密码认证&#xff0c;可以免密远程登录redis服务02漏洞危害1.信息泄露&#xff0c;攻击者可以恶意执行flushall清空数据2.可以通过eval执行…

HTML比较常用的标签

1.全局架构标签&#xff1a;<html><head><title>标题</title><meta charset"utf-8"></head><body>正文部分</body></html><!--注释部分-->2.body标签的属性bgcolor&#xff1a;背景色text:整个网页的颜…

sae项目服务器,基于SAE的游戏服务器: Server on SAE for RGSS Games 部署在SAE上的简易游戏服务器,为用 RMXP/VX/VA 开发的游戏提供网络服务...

本项目已经关闭服务端已经关闭并且不再重启&#xff0c;后续请访问 RGSOS on Gitlab基于SAE的游戏服务器重写服务端逻辑中……暂时无法正常提供服务功能数据库封装封装了 SAE 上的 Memcached&#xff0c;KVDB 和 Storage 到 SAE_IO 类&#xff0c;并引申到两个子类&#xff1a;…

1090 Highest Price in Supply Chain (25)

A supply chain is a network of retailers&#xff08;零售商&#xff09;, distributors&#xff08;经销商&#xff09;, and suppliers&#xff08;供应商&#xff09;-- everyone involved in moving a product from supplier to customer. Starting from one root suppli…