原创

使用Nginx配置来动态解析Nginx/Html目录下文件夹做为二级域名的前缀

温馨提示:
本文最后更新于 2022年08月25日,已超过 608 天没有更新。若文章内的图片失效(无法正常加载),请留言反馈或直接联系我

同一台服务器可能需要部署多个程序,这里使用nginx解析php程序,反向代理tomcat java程序。可以实现动态解析域名,经过Nginx配置来动态解析Nginx/Html目录下的文件夹,做为二级域名。

准备工作:需要提前装好nginx,部署好程序,程序放在nginx/html目录下。

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    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  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

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

    include /etc/nginx/conf.d/*.conf;

}

include /etc/nginx/conf.d/*.conf

1.动态解析,将根目录下的文件夹动态自动解析成二级域名

server {
listen 80;
server_name ~^(?<subdomain>.+).richwit.cn$ www.richwit.cn;
index index.html index.htm index.php;
root /usr/share/nginx/html/$subdomain/;
location ~ .php$ {
    #fastcgi_pass unix:/dev/shm/php-cgi.sock;
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
    #fastcgi_param SCRIPT_FILENAME documentrootdocumentrootfastcgi_script_name;
    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    include fastcgi_params;
    }
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|ico)$ {
    expires 30d;
    }
location ~ .*\.(js|css)?$ {
    expires 7d;
    }
}

2.Java程序 tomcat部署,nginx反向代理

server {
listen 80;
server_name o2o.richwit.cn;
 location / {
   proxy_pass http://localhost:8088;
   root o2o;
   index index.html index.htm index.php;
 }
}

根据解析的域名即可同时运行多个程序

正文到此结束
该篇文章的评论功能已被站长关闭
本文目录