Can
Be Better

在 Nginx 中设定二级目录中的 Laravel

需求问题

公司环境是ubuntu+nginx+php7 然后选用了laravel框架。过去按照惯例,首先要在nginx绑定一个server然后root指向laravel中的public目录。这样是最简单省事的,但是公司的配置是这样的

	ssl on;
	ssl_certificate   cert/1234567890.pem;
    	ssl_certificate_key  cert/1234567890.key;
    	ssl_session_timeout 5m;
	root /var/www/html/;

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

	server_name _;

	
	location / {
		# First attempt to serve request as file, then
		# as directory, then fall back to displaying a 404.
		#try_files $uri $uri/ =404;
		#try_files $uri $uri/ /$query_string;
		try_files $uri $uri/ /index.php?$query_string;
	}

	# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
	#
	location ~ \.php$ {
		include snippets/fastcgi-php.conf;
	#
	#	# With php7.0-cgi alone:
	#	fastcgi_pass 127.0.0.1:9000;
	#	# With php7.0-fpm:
		fastcgi_pass unix:/run/php/php7.0-fpm.sock;
	}

	# deny access to .htaccess files, if Apache's document root
	# concurs with nginx's one
	#
	location ~ /\.ht {
		deny all;
	}

其中的域名只有一个,而且不能够指向laravel中的public目录。所以laravel项目只能以二级或者N级子目录的形式存在。好吧写一份重定向吧。

	location /123/1234/12345/123456/1234567/public {

          rewrite ^/123/1234/12345/123456/1234567/public/(.*)$ /123/1234/12345/123456/1234567/public/index.php?_url=/$1;
          try_files $uri $uri/ /index.php?$query_string;

       }

然后每次访问都要这样输入

https://test.com/123/1234/12345/123456/1234567/public/

且不论静态文件的加载,就单论这个地址傻不傻蠢不蠢。

然后经过一番优化有了如下的配置T_T都快放弃了。

        location ^~ /web {
                alias /var/www/html/123/1234/12345/123456/1234567/public;
                try_files $uri $uri/ @web;

                location ~ \.php {
                        include fastcgi_params;
                        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
                        fastcgi_split_path_info ^(.+\.php)(.*)$;
                        fastcgi_param  SCRIPT_FILENAME /var/www/html/123/1234/12345/123456/1234567/public/index.php;
                }
        }

        location @web {
                 rewrite /web/(.*)$ /web/index.php/$1 last;
        }

 

不开启评论,如有问题疑问请发邮件。i@ltq.im最长的路 » 在 Nginx 中设定二级目录中的 Laravel

评论 抢沙发

评论前必须登录!