Nginx 二级域名入门配置

要解决的问题

一个域名,如果只挂一个站点有点浪费,希望可以
aa.hostname.com -> localhost:8000
bb.hostname.com -> localhost:8001

思路

  • 在 /etc/hosts 底下加入对hostname.com, aa.hostname.com, bb.hostname.com的解析。
  • 二级域名的本质是用proxy_pass来实现的,路径的match导致请求重定向到proxy_pass的地方

方案

这是一个完整的/etc/nginx/nginx.conf,用python -m http.server来做示范了

user www-data;
worker_processes 4;
pid /run/nginx.pid;

events {
    worker_connections 768;
    # multi_accept on;
}

http {

    ##
    # Basic Settings
    ##

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    # server_tokens off;

    # server_names_hash_bucket_size 64;
    # server_name_in_redirect off;

    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    ##
    # Logging Settings
    ##

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    ##
    # Gzip Settings
    ##

    gzip on;
    gzip_disable "msie6";

    # gzip_vary on;
    # gzip_proxied any;
    # gzip_comp_level 6;
    # gzip_buffers 16 8k;
    # gzip_http_version 1.1;
    # gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;

    ##
    # nginx-naxsi config
    ##
    # Uncomment it if you installed nginx-naxsi
    ##

    #include /etc/nginx/naxsi_core.rules;

    ##
    # nginx-passenger config
    ##
    # Uncomment it if you installed nginx-passenger
    ##

    #passenger_root /usr;
    #passenger_ruby /usr/bin/ruby;

    ##
    # Virtual Host Configs
    ##

    server {
        server_name aa.hostname.com;
        listen 80;
        location / {
            proxy_pass  http://127.0.0.1:8000/Documents/;
        }
    }

        server {
        server_name bb.hostname.com;
        listen 80;
        location / {
            proxy_pass  http://127.0.0.1:8001/Documents/;
        }
    }
    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}

#mail {
#   # See sample authentication script at:
#   # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
#
#   # auth_http localhost/auth.php;
#   # pop3_capabilities "TOP" "USER";
#   # imap_capabilities "IMAP4rev1" "UIDPLUS";
#
#   server {
#       listen     localhost:110;
#       protocol   pop3;
#       proxy      on;
#   }
#
#   server {
#       listen     localhost:143;
#       protocol   imap;
#       proxy      on;
#   }
#}

Nginx Road Map

显然只有http协议底下的转发是容易的,需要考虑如果有websocket的情况如何进行转发。
感觉思路应该是大体上一致的。

Ref
Reverse Proxy
Official Admin Guide 值得学的东西太多

时间: 2024-08-09 17:27:09

Nginx 二级域名入门配置的相关文章

Nginx二级域名配置

Nginx二级域名配置模板 域名一:www.hellosr.com 域名二:daxin.hellosr.com 通过upstream进行负载均衡,通过access_log的配置规范化请求日志输出 配置如下: #运行用户 #user www-data; #启动进程,通常设置成和cpu的数量相等 worker_processes 2; #全局错误日志及PID文件 #error_log /home/nginx/logs/error.log; #pid /home/nginx/logs/nginx.pi

nginx二级域名配置自动跳转到一级域名

nginx二级域名配置自动跳转到一级域名 rewrite配置内容: if ($http_host !~ "^www.aaa.com$") { rewrite ^(.*) http://www.aaa.com$1 permanent; } 下方,nginx代理访问项目proxy_pass,及rewrite参考 server { listen 80; server_name www.aaa.com 100.100.100.100; location / { if ($http_host !

Nginx关于个性二级域名的配置

在很多地方,比如说博客网站.我们经常可以会通过网站提供的配置功能,实现个性化的属于个人的二级域名,比如说http://cevin15.oschina.net.当然,OSC的博客目前还没有这个功能. 最近遇到个差不多的需求,网上看了下别人的实现方式,写得不太清楚,自己第一遍即使看懂了,回头看第二遍估计还是不懂.还是要自己测试下,然后记录记录.于是玩了下本地的Nginx,研究怎么去实现这种个性二级域名. 第一时间想到的是通过Nginx的rewrite来实现.配置如下: server { listen

nginx 二级域名问题记录

今天终于把nginx的二级域名配置搞定了,哎之前在测试服务器上弄过一次,不过那个是在本地解析的hosts,把ip指向到域名上就ok,再在nginx.conf里改了下配置就好了,用同样的方法改了正式服务器上的nginx.conf(忘了正式服务器的是域名,这个域名是要DNS解析的)导致耗费了N多时间哇 谨记谨记!如果查了N多的资料,配置文件么有问题,记得DNS解析! 我是一个二级域名写了一个conf,在nginx.conf里引进了所有.conf结尾的文件include 你的配置目录/nginx/co

nginx虚拟域名的配置以及测试验证

1.保证该机器上安装了nginx 未安装请看:centos/linux下的安装Nginx 2.使用root用户编辑配置文件 vim /usr/local/nginx/conf/nginx.conf 3.新建vhost文件夹 cd /usr/local/nginx mkdir vhost 4.在文件中添加这句话(将文件分类便于管理) include vhost/*.conf; 5.测试配置文件 nginx -t 出现如上图则配置完成 6.在vhost上面添加对应的xxx.conf文件即可 例如:

Linux JDK Tomcat Nginx MariaDB 安装,Nginx 多域名转发配置

安装JDK rpm包下载地址(jdk-7u17 ): http://www.oracle.com/technetwork/java/javase/downloads/java-archive-downloads-javase7-521261.html#jdk-7u17-oth-JPR # yum install wget -y JDK下载地址: http://download.oracle.com/otn/java/jdk/7u17-b02/jdk-7u17-linux-x64.rpm?Auth

Nginx 设置域名转向配置

#运行用户 #user www-data; #启动进程,通常设置成和cpu的数量相等 worker_processes 2; #全局错误日志及PID文件 error_log logs/error.log; error_log logs/error.log notice; error_log logs/error.log info; #pid logs/nginx.pid; #工作模式及连接数上限 events { #单个后台worker process进程的最大并发链接数 worker_conn

Nginx 泛解析配置请求映射到多端口实现二级域名访问

由于想实现一个域名放置多个应用运行的目的,而不想通过域名后加端口号方式处理,这种方式处理记起来太麻烦,偷懒党简直不能忍,故而考虑了使用二级域名来处理多个应用同时运行.Google了一番资料并进行了尝试后,进行了总结. 文章开始之前先来理解一下二级域名的概念. 二级域名是指顶级域名之下的域名,在国际顶级域名下,它是指域名注册人的网上名称:在国家顶级域名下,它是表示注册企业类别的符号.我国在国际互联网络信息中心(Inter NIC) 正式注册并运行的顶级域名是CN,这也是我国的一级域名.在顶级域名之

django 项目部署在 Apache 后, 设置二级域名

上一篇文章简单说了怎么把django的项目部署到Apache上. 现在想弄个二级域名,也就是我原来有个域名 www.mysite.com,现在我想弄个 bbs.mysite.com ,该怎么做呢. 要用到 Apache 的虚拟主机配置. 其实Apache的虚拟主机之前也配置过,有时有效,有时又不好用,也不知道个所以然.这次我详细的写一下每一步都要怎么做: 第一步:开启Apache虚拟主机功能: Apache的虚拟主机功能,默认是关闭的.如果要使用这个功能,就要开启虚拟主机功能. 如同上文提到的A