Nginx位置配置为两个方向

时间:2015-04-07 09:45:09

标签: nginx configuration location

我对配置nginx服务器有一点经验,这是我的麻烦。 我正在尝试设置正确的位置。我有两个指示: address.com address.com/api

对于最后的方向(API)我已经设置了位置,它工作正常。 API位于 / var / www / project / api 文件夹中。

root /var/www/project;
index index.php;
server_name localhost;

location /api {
            try_files /api/$uri $uri/ /api/index.php?$query_string;
            fastcgi_pass 127.0.0.1:9000;                
            fastcgi_split_path_info ^/api/(.+\.php)(/.+)$;
            fastcgi_intercept_errors on;
            fastcgi_index index.php;
            include fastcgi_params;
}
location ~ \.php$ {
            try_files $uri =404;
            fastcgi_keep_conn on;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
            fastcgi_buffer_size 32k;
            fastcgi_busy_buffers_size 64k;
            fastcgi_buffers 4 32k;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
    }

现在我需要将address.com的root用户实现为 / var / www / project / website 。在这里,我有一些麻烦。

首先,我做了什么,我曾写过:

 location / {
      alias /var/www/project/website/;
 }

然后我尝试添加许多不同的变体,这是我的最后一个注释。

我把它放在 location / {}

里面
       location ~ ^/(.+\.php)$ {
           alias /var/www/project/website/;
           include /etc/nginx/fastcgi.conf; 
           proxy_intercept_errors on;
           fastcgi_split_path_info ^(.+\.php)(.*)$;
           fastcgi_intercept_errors on;
           fastcgi_pass 127.0.0.1:9000;
           fastcgi_index index.php;
   }

/etc/nginx/fastcgi.conf 文件中我添加了

fastcgi_param PATH_TRANSLATED $document_root$fastcgi_script_name;

我得到的所有时间 403 Forbidden 404 Not found 或在nginx错误中记录日志,例如 / var / www / project /找不到网站/...

是否有人对nginx配置有经验,可以说明如何设置 / website 位置正确?

1 个答案:

答案 0 :(得分:0)

类似的东西:

server {
    listen 80;
    server_name localhost;

    root /var/www/src/website;
    index index.php index.html;

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

    location / {
        try_files $uri $uri/ =404;
    }
    location /test {
        try_files $uri $uri/test.html =404;
    }
    location /api/ {
        alias /var/www/src/api/;
        try_files $uri $uri/ /index.php =404;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include /etc/nginx/fastcgi_params;
    }
    location /pmants {
        root /var/www/src/;
        index index.php index.html index.htm;
        location ~ ^/pmants/(.+\.php)$ {
                try_files $uri =404;
                root /var/www/src/;
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include /etc/nginx/fastcgi_params;
        }

        location ~* ^/pmants/(.+\.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$ {
                root /var/www/src/;
        }
    }
    location ~* \.php {
        include fastcgi_params;

        fastcgi_pass unix:/var/run/php5-fpm.sock;

        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_cache off;
        fastcgi_index index.php;
    }
}