【问题标题】:Nginx locations configuration for two directions两个方向的 Nginx 位置配置
【发布时间】:2015-06-11 20:54:39
【问题描述】:

我对配置 nginx 服务器有一点经验,这是我的麻烦。 我正在尝试设置正确的位置。我有两个指令:address.comaddress.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 Forbidden404 Not found 或在 nginx 错误日志中写入例如 /var/www/project/网站/... 未找到。

有 nginx 配置经验的人可以告诉,如何正确设置 /website 位置?

【问题讨论】:

    标签: nginx configuration location


    【解决方案1】:

    类似的东西:

    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;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-29
      • 2014-05-09
      • 2017-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-08
      • 1970-01-01
      相关资源
      最近更新 更多