>  기사  >  PHP 프레임워크  >  Nginx를 사용하여 Laravel을 로드하는 방법은 무엇입니까?

Nginx를 사용하여 Laravel을 로드하는 방법은 무엇입니까?

藏色散人
藏色散人앞으로
2020-01-30 19:41:452218검색

Nginx를 사용하여 Laravel을 로드하는 방법은 무엇입니까?

프로젝트 환경 php7.2, nginx, Laravel, WeChat 공개 계정 애플리케이션이 개발되었습니다. 현재 방문량이 증가함에 따라 단일 서버로는 수요를 충족할 수 없으므로 nginx를 사용하여 이를 로드합니다.

다음은 현재 사용 가능한 솔루션입니다.

세션 공유 문제 참조: Laravel은 Redis를 사용하여 세션을 공유합니다(자세한 코드 설명)

두 개의 웹 서버가 A:10.0.0.2, B:10.0.0.3 있습니다. , 시스템의 도메인 이름은 www.c.com입니다. A를 역방향 프록시 서버로 사용하세요A:10.0.0.2 , B:10.0.0.3,系统的域名为 www.c.com,使用A使用为反向代理服务器

A服务器的nginx配置

server {
listen 81;
server_name _;
index index.html index.htm index.php;
access_log /data/wwwroot/wwwlogs/www_nginx.log combined;
root /data/wwwroot/www/public;
location / {
    try_files $uri $uri/ /index.php?$query_string;
}
location ~ .*\.(php|php5)?$ {
   fastcgi_pass unix:/dev/shm/php-cgi.sock;
    fastcgi_index index.php;
    include fastcgi.conf;
    }
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|ico)$ {
    expires 30d;
    access_log off;
    }
location ~ .*\.(js|css)?$ {
    expires 7d;
    access_log off;
    }
}

B服务器nginx设置

server {
listen 80;
server_name -;
index index.html index.htm index.php;
access_log /data/wwwlogs/www_nginx.log combined;
root /data/wwwroot/www/public;
location / {
    try_files $uri $uri/ /index.php?$query_string;
}
location ~ .*\.(php|php5)?$ {
 
   fastcgi_pass unix:/dev/shm/php-cgi.sock;
    fastcgi_index index.php;
    include fastcgi.conf;
    }
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|ico)$ {
    expires 30d;
    access_log off;
    }
location ~ .*\.(js|css)?$ {
    expires 7d;
    access_log off;
    }
}

A 服务器上的反向代理服务器设置

upstream backend{
    ip_hash;
    server 127.0.0.1:81;
    server 10.0.0.3;
}
server {
listen 80;
server_name www.c.com;
index index.html index.htm index.php;
access_log /data/wwwroot/wwwlogs/www_nginx.log combined;
root /data/wwwroot/www/public;
location / {
    proxy_pass http://backend;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header REMOTE-HOST $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location ~ .*\.(php|php5)?$ {
    fastcgi_pass unix:/dev/shm/php-cgi.sock;
    fastcgi_index index.php;
    include fastcgi.conf;
    }
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|ico)$ {
    expires 30d;
    access_log off;
    }
location ~ .*\.(js|css)?$ {
    expires 7d;
    access_log off;
    }
}

如果是做微信公众号,使用了easywechat,需要把backend 代换为你的www.c.com 这样才能支持微信授权。

微信的access_token需要使用redis共享,easywechat默认使用laravel 的缓存,所以需要把.env的缓存改成使用redis

CACHE_DRIVER=redis

Laravel 中使用 Request::getClientIp()获取到的IP不是真实的IP,需要修改app/Providers/AppServiceProvider.php

A 서버의 nginx 구성

  public function boot()
    {
        \Request::setTrustedProxies(['127.0.0.1','10.0.0.2']);
    }
B 서버 nginx 설정rrreee

서버의 역방향 프록시 서버 설정🎜rrreee🎜WeChat 공개 계정을 만들고 easywechat을 사용하는 경우 백엔드를 다음으로 교체해야 합니다. 귀하의 www. 이러한 방법으로만 c.com이 WeChat 인증을 지원할 수 있습니다. 🎜🎜WeChat의 access_token은 redis를 사용하여 공유해야 합니다. Easywechat은 기본적으로 laravel의 캐시를 사용하므로 redis를 사용하려면 .env 캐시를 변경해야 합니다.🎜🎜CACHE_DRIVER=redis🎜🎜에서 요청 사용 Laravel: :getClientIp()얻은 IP는 실제 IP가 아닙니다. 신뢰할 수 있는 프록시 서버(127.0.0.1)의 IP를 설정하려면 app/Providers/AppServiceProvider.php를 수정해야 합니다. , 10.0.0.2), Request::getClientIp()를 사용하여 실제 IP를 얻을 수 있습니다. 🎜rrreee🎜laravel 프레임워크와 관련된 더 많은 기술 기사를 보려면 🎜laravel 튜토리얼🎜 칼럼을 방문하세요! 🎜

위 내용은 Nginx를 사용하여 Laravel을 로드하는 방법은 무엇입니까?의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
이 기사는 cnblogs.com에서 복제됩니다. 침해가 있는 경우 admin@php.cn으로 문의하시기 바랍니다. 삭제