【问题标题】:React client not connecting to Symfony API using NginxReact 客户端未使用 Nginx 连接到 Symfony API
【发布时间】:2022-02-05 03:55:47
【问题描述】:

我已经设置了一个 AWS Lightsail 实例,我正在尝试部署我的 Symfony API 和连接到该 API 的 React 客户端。

我可以访问 React 客户端,但无法访问 Symfony API,并且显示 500 内部错误。

这是我的 Ningx 配置文件:

```
server {
    listen 80;
    listen [::]:80;

    server_name name my_server_ip;

    return 301 https://$server_name$request_uri;
}

server {
    listen 443 ssl;
    listen [::]:443 ssl;
    include snippets/self-signed.conf;
    include snippets/ssl-params.conf;    

    access_log /var/log/nginx/symfony-project.log;
    error_log /var/log/nginx/symfony-project-error.log error;

    server_name name my_server_ip;    

    client_max_body_size 100m;

    location /api {
        index index.php;
        root /var/www/html/symfony-project/public;
        try_files $uri /index.php$is_args$args;
    }

    location / {
        root /var/www/html/react-project/build;
        try_files $uri $uri/ =404;
        index index.html;
    }

    location ~ \.php {
        try_files $uri /index.php$is_args$args;
        fastcgi_pass unix:/var/run/php/php8.0-fpm.sock; 
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;    
        #fastcgi_param SCRIPT_NAME $fastcgi_script_name;
        #fastcgi_index index.php;include fastcgi_params;  
        fastcgi_split_path_info ^(.+\.php)(/.+)$;    
    }    

    location ~ /\.(?:ht|git|svn) {
        deny all;
    }  
}

server {
    listen 3000;
    listen [::]:3000;

    server_name name my_server_ip;

    return 301 https://$server_name$request_uri;    
}

在我的 Ningx 错误日志文件中,我收到了这个错误:

2022/02/04 19:07:34 [错误] 5100#5100: *72 重写或内部重定向周期,同时内部重定向到“/index.php”,客户端:93.202.51.115,服务器:symfony-project,请求:“GET /api HTTP/1.1”,主机:“52.59.39.188”

请给点建议,谢谢。

【问题讨论】:

    标签: php reactjs symfony nginx


    【解决方案1】:

    location ~ \.php 部分中,这是我的配置的样子,并且运行良好:

    location ~ \.php {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param SCRIPT_NAME $fastcgi_script_name;
        fastcgi_index index.php;
        fastcgi_pass unix:/var/run/php/php8.0-fpm.sock;
    }
    

    注意:如果您对配置进行任何更改,请务必重新启动 nginx 和 php-fpm:

    service nginx restart
    service php8.0-fpm restart
    

    【讨论】:

    • 我之前尝试过此配置,但它显示 404 错误。还是谢谢你:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-04
    • 1970-01-01
    • 1970-01-01
    • 2016-12-18
    • 2016-12-08
    • 2015-08-19
    相关资源
    最近更新 更多