Nginx 在 Apache 上与 Laravel 一起使用时出现 rewrite_mod 问题

Nginx 在 Apache 上与 Laravel 一起使用时出现 rewrite_mod 问题

我在 Apache 上运行 Nginx。

我 Nginx 监听端口80并将其代理到8080Apache 监听的端口 。

到目前为止,它适用于文件的直接 URL,例如domain.comdomain.com/index.php

但如果我去domain.com/test,它404 Not Found nginx/1.4.6 (Ubuntu)甚至会说我已经设置了route一个/test

这是我的域的 nginx 配置:

server {
        listen 80;
        access_log /var/www/site.com/logs/nginx.access.log;
        error_log /var/www/site.com/logs/nginx.error.log;
        root /var/www/site.com/public_html/public;
        index index.php index.html;
        server_name site.com;
        location \ {
                try_files $uri $uri/ index.php/$uri;
        }
        location ~* ^.*\.php$ {
                if (!-f $request_filename) {
                        return 404;
                }
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header Host $host;
                proxy_pass http://127.0.0.1:8080;
        }
        location ~ /\.(ht|git) {
                deny all;
        }
}

以下是该域的 Apache 配置:

<VirtualHost *:8080>
        ServerName site.com
        DocumentRoot /var/www/site.com/public_html/public/
        CustomLog /var/www/site.com/logs/apache.access.log common
        ErrorLog /var/www/site.com/logs/apache.error.log
</VirtualHost>

答案1

既然您已经在使用 nginx,为什么不放弃 Apache+mod_php 而使用 php-fpm?这样就不需要代理了。

安装后php5-fpm,添加此虚拟主机,并使用sudo ln -s ../sites-available/example.com.conf /etc/nginx/sites-enabled/

# /etc/nginx/sites-available/example.com.conf
server {
    listen 80;    
    server_name     example.com;
    root            /var/www/example.com/public_html/public;

    access_log      /var/www/example.com/logs/access.log;
    error_log       /var/www/example.com/logs/error.log;

    # Disallow access to hidden files (.htaccess, .git, etc.)
    location ~ /\. {
        deny all;
    }

    # Some locations will never contain PHP files, handle it in the server
    location ~ ^/(robots.txt|favicon.ico)(/|$) {
        try_files $uri =404;
    }

    location = /index.php {
        # Disable direct access to the source code of index.php. If you have a
        # Laravel route for '/index.php', copy the @php_router block below.
        return 404;
    }

    location / {
        # Try to serve the static files, otherwise call into PHP
        try_files $uri @php_router;
    }

    location @php_router {
        include fastcgi_params;
        # If you move index.php outside public/, adjust it here.
        fastcgi_param SCRIPT_FILENAME $document_root/index.php;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
    }
}

答案2

我明白了。这是那些想做同样事情的人真正应该知道的事情。以下是我的分享。

我找到了以下解决方案:

server {
        listen 80;
        access_log /var/www/site.com/logs/nginx.access.log;
        error_log /var/www/site.com/logs/nginx.error.log;
        root /var/www/site.com/public_html/public;
        index index.php index.html;
        server_name site.com;

        location / {
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header Host $host;
                proxy_pass http://127.0.0.1:8080;
        }
        location ~* ^.*\.php$ {
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header Host $host;
                proxy_pass http://127.0.0.1:8080;
        }
        location ~ /\.(ht|git) {
                deny all;
        }
}

首先,我添加了代理设置location / {,否则它可能不会将您发送到 Apache。

我接下来要做的是try_files从所有位置删除,因为 Apache 用于.htaccess配置这一点。

这就是我的解决方案。

相关内容