[Nginx] 单机环境的多应用配置
# 服务层
# https://github.com/farwish/alconservice
# alconservice.conf
server {
listen 8090;
root /home/www/alconService/public;
location / {
index index.html index.htm index.php;
try_files $uri $uri/ /index.php?_url=$uri&$args;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location /favicon.ico {
log_not_found off;
access_log off;
}
access_log logs/alconservice.access.log;
}
# 前端应用
# front.yk.conf
server {
listen 80;
server_name front.kkys.com;
root /home/www/web;
location / {
index index.html index.htm index.php;
}
location /api/ {
proxy_pass http://dev.kkys.com/yk/;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
#fastcgi_param PATH_INFO $fastcgi_path_info;
#fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
#fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
# 社区接口
# yk.up
server {
listen 81;
root /home/www/Yk/public;
location / {
index index.html index.htm index.php;
try_files $uri $uri/ /index.php?_url=$uri&$args;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location /favicon.ico {
log_not_found off;
access_log off;
}
access_log logs/yk.access.log;
}
# 检索接口
# https://github.com/farwish/alconseek
# speed.up
server { listen 82; root /home/www/speed/public; location / { index index.html index.htm index.php; try_files $uri $uri/ /index.php?_url=$uri&$args; } location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } location /favicon.ico { log_not_found off; access_log off; } access_log logs/speed.access.log; }
# 百科接口
# baike.up
server { listen 83; root /home/www/baike/public; location / { index index.html index.htm index.php; try_files $uri $uri/ /index.php?_url=$uri&$args; } location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } location /favicon.ico { log_not_found off; access_log off; } access_log logs/baike.access.log; }
# 后端接口总代理
# proxy-all.conf
# 注意路径: 当前文件由nginx.conf引用
include vhost/*.up;
upstream yk-backend {
server 127.0.0.1:81;
}
upstream speed-backend {
server 127.0.0.1:82;
}
upstream baike-backend {
server 127.0.0.1:83;
}
server {
listen 80;
server_name dev.kkys.com;
location / {
root /home/www;
index index.html index.htm index.php;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
# 结尾不加/,访问记录是 //frontend/index/detail/1212
# 加了/,访问记录是 /frontend/index/detail/1212
location /yk/ {
proxy_pass http://yk-backend/;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
}
location /speed/ {
proxy_pass http://speed-backend/;
}
location /baike/ {
proxy_pass http://baike-backend/;
}
}
# 用户中心
# ucenter.conf
server {
listen 8092;
root /home/www/ucenter-front;
location / {
index index.html index.htm index.php;
try_files $uri $uri/ /index.php?_url=$uri&$args;
}
}
server {
listen 8093;
root /home/www/ucenter/public;
location / {
index index.html index.htm index.php;
try_files $uri $uri/ /index.php?_url=$uri&$args;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
upstream ucenter-front {
server 127.0.0.1:8092;
}
upstream ucenter {
server 127.0.0.1:8093;
}
server {
listen 80;
server_name ucenter.com;
location / {
index index.html index.htm index.php;
try_files $uri $uri/ /index.php?_url=$uri&$args;
}
location /favicon.ico {
log_not_found off;
access_log off;
}
location /pcenter/ {
proxy_pass http://ucenter-front/;
}
location /ucenter/ {
proxy_pass http://ucenter/;
}
}
以上 location 的配置是针对 phalcon 框架而言,下面是针对 symfony 应用的。
symfony.conf
server {
listen 80;
server_name symfony.com;
root /home/www/symfony/web;
location / {
index index.html index.htm index.php;
try_files $uri /app.php$is_args$args;
}
# DEV
location ~ ^/(app_dev|config)\.php(/|$) {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
# PROD
location ~ ^/app\.php(/|$) {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
# symfony.com/app.php/some-path
# Remove the internal directive to allow URIs like this.
#internal;
}
access_log logs/symfony.access.log;
}
小结:
多应用基于项目拆分的场景或思想,独立开发、独立部署,可以做到互不影响,如同前后端分离一样。
这里大家会发现,反向代理不仅可以简化多应用的部署,还能做到负载均衡,只需增加 upstream机器 就能横向扩展。
其实也完全可以 以大模块的方式整合成一个强大应用,从易维护上来讲更容易一点。两种方式都各有优势。
Refer:Nginx单机配置