首页 > 解决方案 > Yii2 + Nginx(代理)+ Apache(后台)

问题描述

我正在学习 NGINX,我需要使用 NGINX 设置前端网络服务器和使用 Apache 设置后端网络服务器.htaccess

这是内容/etc/nginx/sites-available/my_test5.loc

server {
  charset utf-8;
  client_max_body_size 128M;

  listen 80;

  server_name my_test5.loc;
  root        /var/www/my_test5.loc/web;
  #root /var/www/my_test5.loc;
  index       index.php;


  location / {
    try_files $uri $uri/ /index.php$is_args$args;
  }

  # deny accessing php files for the /assets directory
  location ~ ^/assets/.*\.php$ {
    deny all;
  }

  location ~ \.php$ {    
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $remote_addr;
    proxy_set_header Host $host;
    proxy_pass http://127.0.0.1:8080;
  }

  location ~* /\. {
    #  deny all;
    allow all;
  }
}

这是内容/etc/apache2/sites-available/my_test5.loc.conf

<VirtualHost *:8080>
  <Directory /var/www/my_test5.loc/>
    Options Indexes FollowSymLinks MultiViews
    AllowOverride All 
    Order allow,deny
    allow from all 
  </Directory>
  ServerAdmin admin@my_test5.loc
  ServerName my_test5.loc
  ServerAlias www.my_test5.loc
  DocumentRoot /var/www/my_test5.loc/web/
  ErrorLog ${APACHE_LOG_DIR}/error.log
  CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

现在我所做的只是主页。例如,如果我尝试导航到站点/关于我不能。我做错了什么以及如何使用这两个网络服务器?

标签: apache.htaccessnginxproxy

解决方案


我认为您的设置只是index.php因为您的location / {}指令而正确指向,请尝试编辑您以更宽容地允许您的 Apache 访问更改此块以通过它并将特定块添加到其他类型的文件,如资产。

location / {
  proxy_set_header X-Real-IP $remote_addr;
  proxy_set_header X-Forwarded-For $remote_addr;
  proxy_set_header Host $host;
  proxy_pass http://127.0.0.1:8080;
}

location ~* \.(jpg|jpeg|png|gif|ico|css|js|ttf|ttc|otf|eot|woff|woff2)$ {
  try_files $uri $uri/
}

这样,NGINX 为您的静态文件提供服务,而 Apache 为其他所有内容提供服务。


推荐阅读