JAVA和Nginx 教程大全

网站首页 > 精选教程 正文

设置Tengine/Nginx多域名多站点共享一台服务器

wys521 2024-09-29 22:49:28 精选教程 22 ℃ 0 评论

前言

今天给各位介绍下Tengine/Nginx服务器绑定多个域名多个网站的配置方法。

Tengine/Nginx配置文件

  • Tengine/Nginx配置文件在默认安装目录下的conf文件夹下,你也可以在终端下使用“whereis nginx”查找它的安装目录。
  • 查找nginx安装目录 如下我的nginx配置文件路径即为: /usr/local/nginx/conf/nginx.conf
[root@localhost ~]# whereis nginx
nginx: /usr/local/nginx
[root@localhost ~]#

Nginx的配置文件

vim /usr/local/nginx/conf/nginx.conf #打开nginx配置文件命令 你也可以使用你喜欢的编辑工具如vi emacs gedit

原文件

#以下为Nginx 配置文件nginx.conf默认内容,已加注解。
user www www-data; #以www会员和www-data会员组运行nginx
worker_processes 1; #最大进程数,一般设为cpu核心数量 如你是4核cpu 可以设为4
#error_log logs/error.log; #指定错误日志文件路径,默认当前配置文件的父级目录logs下的error.log
#error_log logs/error.log notice; #指定错误日志文件路径并指定为只记录notice级别错误
#error_log logs/error.log info; #指定错误日志文件路径并指定为只记录info级别错误
#pid logs/nginx.pid; ##记录nginx运行时的进程ID
events {
 worker_connections 1024; #允许的最大连接数即tcp连接数
}
# load modules compiled as Dynamic Shared Object (DSO)
# 动态模块加载(DSO)支持。加入一个模块不再需要重新编译整个Tengine 这个是Tengine特有的
#dso {
# load ngx_http_fastcgi_module.so; #fastcgi模块
# load ngx_http_rewrite_module.so; #URL重写模块
#}
http {
 include mime.types; #设定mime类型,类型由conf目录下mime.type文件定义
 default_type application/octet-stream; #默认为 任意的二进制数据 
 ## 可配置日志格式: $remote_addr访客ip
 ## $remote_user已经经过Auth Basic Module验证的用户名
 ## $time_local访问时间
 ## $request请求的url 
 ## $body_bytes_sent 传送页面的字节数 $http_referer访问来源
 #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
 # '$status $body_bytes_sent "$http_referer" '
 # '"$http_user_agent" "$http_x_forwarded_for"';
 #access_log logs/access.log main; #访问记录日志
 sendfile on; #开启高效文件传输模式 注意:如果图片显示不正常把这个改成off。
 #tcp_nopush on; #防止网络阻塞
 #keepalive_timeout 0;
 keepalive_timeout 65; #长连接超时时间,单位是秒
 #gzip on; #开启gzip压缩
 server {#虚拟主机的配置
 listen 80; #监听80端口
 server_name localhost; #绑定域名可以有多个,用空格隔开
 #charset koi8-r; #字符编码 可设为 utf-8
 #access_log logs/host.access.log main; #访问记录日志
 location / { ##网站根目录设置在这里
 root html; #配置文件父级html目录,可以设到其它目录如/home/www目录,注意目录的所有者和权限 本文开头处user的信息
 index index.html index.htm; #默认索引文件,从左到右,如:index.php index.html index.htm 空格分开
 }
 #error_page 404 /404.html; #指定404错误文件位置 root指定目录下的404.html 以下50x文件同理
 # redirect server error pages to the static page /50x.html 
 #
 error_page 500 502 503 504 /50x.html; ##服务器50x得的错误都跳转到html/50x.html文件 
 location = /50x.html {
 root html;
 }
 # proxy the PHP scripts to Apache listening on 127.0.0.1:80
 # 配置处理php文件,需要安装PHP
 #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; ##禁止使用.htaccess文件
 #}
 }
 #
 #server {
 # listen 8000;
 # listen somename:8080;
 # server_name somename alias another.alias;
 # location / {
 # root html;
 # index index.html index.htm;
 # }
 #}
 # HTTPS server
 #
 #server {
 # listen 443 ssl;
 # server_name localhost;
 # ssl_certificate cert.pem;
 # ssl_certificate_key cert.key;
 # ssl_session_cache shared:SSL:1m;
 # ssl_session_timeout 5m;
 # ssl_ciphers HIGH:!aNULL:!MD5;
 # ssl_prefer_server_ciphers on;
 # location / {
 # root html;
 # index index.html index.htm;
 # }
 #}
 }

修改后

###以下为修改过的nginx.conf配置文件,已加注解。
user www www-data; #以www会员和www-data会员组运行nginx
worker_processes 1; #最大进程数,一般设为cpu核心数量 如你是4核cpu 可以设为4
error_log logs/error.log; #指定错误日志文件路径,默认当前配置文件的父级目录logs下的error.log
#error_log logs/error.log notice; #指定错误日志文件路径并指定为只记录notice级别错误
#error_log logs/error.log info; #指定错误日志文件路径并指定为只记录info级别错误
pid logs/nginx.pid; ##记录nginx运行时的进程ID
events {
 use epoll; #新加 提高nginx的性能,限Linux下使用
 worker_connections 1024; #允许的最大连接数即tcp连接数
}
# load modules compiled as Dynamic Shared Object (DSO)
# 动态模块加载(DSO)支持。加入一个模块不再需要重新编译整个Tengine 这个是Tengine特有的
#dso {
# load ngx_http_fastcgi_module.so; #fastcgi模块
# load ngx_http_rewrite_module.so; #URL重写模块
#}
http {
 include mime.types; #设定mime类型,类型由conf目录下mime.type文件定义
 default_type application/octet-stream; #默认为 任意的二进制数据 
 ##可配置日志格式: $remote_addr访客ip
 ## $remote_user已经经过Auth Basic Module验证的用户名
 ## $time_local访问时间
 ## $request请求的url 
 ## $body_bytes_sent 传送页面的字节数
 ## $http_referer访问来源
 #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
 # '$status $body_bytes_sent "$http_referer" '
 # '"$http_user_agent" "$http_x_forwarded_for"';
 #access_log logs/access.log main; #访问记录日志
 sendfile on; #开启高效文件传输模式 注意:如果图片显示不正常把这个改成off。
 #tcp_nopush on; #防止网络阻塞
 #keepalive_timeout 0;
 keepalive_timeout 65; #长连接超时时间,单位是秒
 gzip on; #开启gzip压缩
 ## 新加 include 项引入/usr/local/nginx/vhosts/目录下所有.conf结尾的虚拟机配置文件
 include /usr/local/nginx/vhosts/*.conf
 }

建立虚拟机配置目录

  • 在nginx安装目录中新建目录虚拟机配置目录:vhosts
mkdir /usr/local/nginx/vhosts/

新建网站配置文件

vim /usr/local/nginx/vhosts/test.conf
##www.test.my 的配置文件
 server { #虚拟主机的配置
 listen 80; #监听80端口
 server_name www.test.my www.test.my; #绑定域名可以有多个,用空格隔开
 #charset koi8-r; #字符编码 可设为 utf-8
 charset utf-8;
 #access_log logs/host.access.log main; #访问记录日志
 location / { ##网站根目录设置在这里
 root html; #配置文件父级html目录,可以设到其它目录如/home/www目录,注意目录的所有者和权限 本文开头处user的信息
 index index.html index.htm; #默认索引文件,从左到右,如:index.php index.html index.htm 空格分开
 }
 #error_page 404 /404.html; #指定404错误文件位置 root指定目录下的404.html 以下50x文件同理
 # redirect server error pages to the static page /50x.html 
 #
 error_page 500 502 503 504 /50x.html; ##服务器50x得的错误都跳转到html/50x.html文件 
 location = /50x.html {
 root html;
 }
 # proxy the PHP scripts to Apache listening on 127.0.0.1:80
 # 配置处理php文件,需要安装PHP
 #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; ##禁止使用.htaccess文件
 #}
 }

配置多个网站

  • 同理,在vhosts目录下生成多个站点的配置文件即可完成多站点多域名共享一台主机。
#如再配置一个域名为 yuntheme.com站点的配置文件 
 vim /usr/local/nginx/vhosts/yuntheme.conf
 #复制刚才的test.conf的内容,只要像下面修改就行了
location / { ##网站根目录设置在这里
 root /usr/local/nginx/html/yuntheme; #修改此处
 index index.html index.htm;
 }

完成配置,重启Nginx

/usr/local/nginx/sbin/nginx -s reload

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表