2016-12-27 72 views
1

我配置了http-> https重定向+非www-> www重定向。 我想要排除兩個路徑,以便它們不會被重定向到https。 我已經嘗試了很多可能的配置,我要麼獲得一個404或我重定向到https版本。帶位置異常的nginx/php-fpm - 404

下面是當前的配置,試圖獲得一個/ LOC2 /路徑時,它返回一個404(#curl http://www.server.dev/loc2/18a9BM4Lay):

server { 
    listen 80; 
    listen [::]:80; 
    server_name server.dev; 
    location/{ 
return 301 https://$server_name$request_uri; 
    } 
    location /loc1/ { 
     try_files $uri $uri/ /index.php?$args; 

    } 
    location /loc2/ { 
     try_files $uri $uri/ /index.php?$args; 
    } 
} 

server { 
    listen 80; 
    listen [::]:80; 
    server_name www.server.dev; 
    root /var/www/web/server/public; 

    location/{ 
# return 301 https://$server_name$request_uri; 
    } 

     location ^~ /loc1/ { 
#  root /var/www/web/server/public; 
    index index.php; 
#  try_files $uri $uri/ /index.php?$args; 
     include pool_web.conf; 
     } 

     location ^~ /loc2/ { 
#  root /var/www/web/server/public; 
    index index.php; 
#  try_files $uri $uri/ /index.php?$args; 
     location ~ \.php$ { 
       # regex to split $uri to $fastcgi_script_name and $fastcgi_path 
       fastcgi_split_path_info ^(.+\.php)(/.+)$; 
       # Check that the PHP script exists before passing it 
       try_files $fastcgi_script_name =404; 
       # Bypass the fact that try_files resets $fastcgi_path_info 
       # see: http://trac.nginx.org/nginx/ticket/321 
       set $path_info $fastcgi_path_info; 
       fastcgi_param PATH_INFO $path_info; 
       include fastcgi.conf; 
       fastcgi_read_timeout 360s; 
       fastcgi_intercept_errors on; 

       fastcgi_pass unix:/var/run/server-php7.0-fpm.sock; 
    } 
# include pool_web.conf; 

     } 

     } 


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

    listen 443 ssl http2; 

    ssl_certificate /etc/ssl/server.crt; 
    ssl_certificate_key /etc/ssl/server.key; 

    server_name server.dev; 
    rewrite ^ $scheme://www.server.dev$request_uri? permanent; 
} 

server { 
# listen 80; 

    listen 443 ssl http2; 

    ssl_certificate /etc/ssl/server.crt; 
    ssl_certificate_key /etc/ssl/server.key; 

    server_name www.server.dev; 

    root /var/www/web/server/public; 

    index index.php; 

    location/{ 
     try_files $uri $uri/ /index.php?$args; 
    } 

    location /images/ { 
     try_files $uri =404; 
    } 

    location ~ \.php$ { 
     include pool_web.conf; 
    } 

    location ~ \.(css|htc|less|js|js2|js3|js4)$ { 
     expires 31536000s; 
     add_header Pragma "public"; 
     add_header Cache-Control "max-age=31536000, public"; 
    } 

    location ~ \.(asf|asx|wax|wmv|wmx|avi|bmp|class|divx|doc|docx|eot|exe|gif|gz|gzip|ico|jpg|jpeg|jpe|mdb|mid|midi|mov|qt|mp3|m4a|mp4|m4v|mpeg|mpg|mpe|mpp|otf|odb|odc|odf|odg|odp|ods|odt|ogg|pdf|png|pot|pps|ppt|pptx|ra|ram|svg|svgz|swf|tar|tif|tiff|ttf|ttc|wav|wma|wri|woff|xla|xls|xlsx|xlt|xlw|zip)$ { 
     expires 31536000s; 
     add_header Pragma "public"; 
     add_header Cache-Control "max-age=31536000, public"; 
    } 
} 
+0

是否有一個名爲'/ var/www/web/server/public/loc2/18a9BM4Lay'的文件?或者你期望別的事情發生? –

+0

該文件不存在。而index.php處理/ loc2 /路徑。 – syst0m

回答

0

您需要添加一個try_files語句來定義的默認處理程序。 index指令僅適用於指定目錄。

例如:

location ^~ /loc2/ { 
    try_files $uri $uri/ /loc2/index.php; 
    ... 
} 

詳見this document

+0

2.'location ^〜/ loc2/{ try_files $ uri $ uri//index.php?args; \t ... \t} ' 重定向到https: #curl http://www.server.dev/loc2/18ayBMOLVy -L -k -I HTTP/1.1 301永久移動 服務器:nginx/1.10.1(Ubuntu) 日期:2016年12月27日星期二15:43:14 GMT Content-Type:text/html Content-Length:194 Connection:keep-alive Location:https://www.server .dev/loc2/18ayBMOLVy HTTP/1.1 200 OK – syst0m

+0

1.'location ^〜/ loc2/{try_files $ uri $ uri//loc2/index.php?args; ...}產生:* 173重寫或內部重定向週期,同時內部重定向到「/loc2/index.php」,客戶端:127.0.0.1,服務器:www.server.dev,請求:「GET/loc2/18ayBMOLVy HTTP/1.1「,主機:」www.server.dev「' - – syst0m