2017-04-24 92 views
-1

我有SF3應用程序,它在apache2上運行良好,但是當我使用nginx http://localhost/SF3-REST-USER-JWT/web/app_dev.php時,運行也很好,生成一個起始頁面。當我嘗試使用/ api之類的其他路由時,nginx給了我404。看起來,我的配置中的nginx根本不喜歡漂亮的路由。它尋找/ api目錄Symfony 3和nginx

2017/04/24 21:51:42 [error] 23683#23683:* 2 open()「/ var/www/html/SF3-REST-USER-JWT/web/app_dev.php/api「失敗(20:不是目錄),客戶端:127.0.0.1,服務器:_,請求:」GET /SF3-REST-USER-JWT/web/app_dev.php/api HTTP/1.1「,主機:「localhost」

爲什麼?我該怎麼做?

listen 80 default_server; listen [::]:80 default_server;

# SSL configuration 
# 
# listen 443 ssl default_server; 
# listen [::]:443 ssl default_server; 
# 
# Note: You should disable gzip for SSL traffic. 
# See: https://bugs.debian.org/773332 
# 
# Read up on ssl_ciphers to ensure a secure configuration. 
# See: https://bugs.debian.org/765782 
# 
# Self signed certs generated by the ssl-cert package 
# Don't use them in a production server! 
# 
# include snippets/snakeoil.conf; 

root /var/www/html; 

# Add index.php to the list if you are using PHP 
index index.php index.html index.htm index.nginx-debian.html; 

server_name _; 


location ~ ^/(app_dev|config)\.php(/|$) { 
     include snippets/fastcgi-php.conf; 
     fastcgi_pass unix:/var/run/php7.1-fpm.sock; 
     fastcgi_split_path_info ^(.+\.php)(/.*)$; 
     include fastcgi_params; 
    # When you are using symlinks to link the document root to the 
    # current version of your application, you should pass the real 
    # application path instead of the path to the symlink to PHP 
    # FPM. 
    # Otherwise, PHP's OPcache may not properly detect changes to 
    # your PHP files (see https://github.com/zendtech/ZendOptimizerPlus 
    # for more information). 
     fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name; 
     fastcgi_param DOCUMENT_ROOT $realpath_root; 
} 

nginx.conf

user www-data; 
worker_processes auto; 
pid /run/nginx.pid; 

events { 
    worker_connections 768; 
    # multi_accept on; 
} 

http { 

    ## 
    # Basic Settings 
## 

sendfile on; 
tcp_nopush on; 
tcp_nodelay on; 
keepalive_timeout 65; 
types_hash_max_size 2048; 
# server_tokens off; 

# server_names_hash_bucket_size 64; 
# server_name_in_redirect off; 

include /etc/nginx/mime.types; 
default_type application/octet-stream; 

## 
# SSL Settings 
## 

ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE 
ssl_prefer_server_ciphers on; 

## 
# Logging Settings 
## 

access_log /var/log/nginx/access.log; 
error_log /var/log/nginx/error.log; 

## 
# Gzip Settings 
## 

gzip on; 
gzip_disable "msie6"; 

# gzip_vary on; 
# gzip_proxied any; 
# gzip_comp_level 6; 
# gzip_buffers 16 8k; 
# gzip_http_version 1.1; 
# gzip_types text/plain text/css application/json       application/javascript text/xml application/xml application/xml+rss  text/javascript; 

## 
# Virtual Host Configs 
## 

include /etc/nginx/conf.d/*.conf; 
include /etc/nginx/sites-enabled/*; 
} 
+0

您是否試過本指南? http://symfony.com/doc/current/setup/web_server_configuration.html#nginx –

+0

這正是我所做的。 – Vladimir

+0

這只是與sf問題,因爲我的其他wp應用程序工作在nginx – Vladimir

回答

0
Symfony doc gives us fastcgi_pass unix:/var/run/php5-fpm.sock 

但在Ubuntu 16.04應該是fastcgi_pass UNIX:/run/php/php5-fpm.sock。那是我的問題。

0

正如你在Symfony's doc中看到的,root應該指向web目錄。

我想,那/var/www/html是你的整個本地主機服務器的根,你可能無法改變它,因爲你有其他項目,你會無法訪問它們。

沒有改變這個root值,你有兩個選擇。

第一個是修復location部分所以不是:

位置〜^ /(app_dev |配置).PHP(/ | $){

它應該是這樣的:

location ~ ^/{path_relative_to_root}/(app_dev|config)\.php(/|$) { 

此基礎上,你試過網址http://localhost/SF3-REST-USER-JWT/web/app_dev.php的信息,該路徑可能是/SF3-REST-USER-JWT/web/,所以它應該是:

location ~ ^/SF3-REST-USER-JWT/web/(app_dev|config)\.php(/|$) { 

其次是爲此項目創建單獨的虛擬主機,因此它可以具有完全獨立的域(例如, symfonyapp.local或其他)。

要做到這一點,您應該在/etc/nginx/sites-available/中創建單獨的配置文件,其內容類似於Symfony文檔示例中所示的內容。只需要改變根路徑和域名。當然,您需要通過在sites_enabled中創建符號鏈接來啓用此nginx網站。

然後,您還需要將此域添加到您的操作系統中的hosts文件。

在這種情況下,您將可以通過簡單的http://symfonyapp.local/app_dev.php訪問您的網站。

+0

謝謝,但位置〜^/SF3-REST-USER-JWT/web /(app_dev | config)\ .php(/ | $)它給我502甚至在現在的第一種情況下壞網關 – Vladimir

+0

我不知道你**精確的**服務器配置,所以我不能給你100%準備好的解決方案。 –