Nginx 单域名部署前台、后台和接口端

增配置

cd /etc/nginx/sites-available/
touch xiaolaiwo.com.conf
cd ../site-enabled/
ln -s ../sites-available/xiaolaiwo.com.conf

修改配置文件xiaolaiwo.com.conf:

server {
    	listen 443 ssl default_server;
    	server_name www.xiaolaiwo.com *.xiaolaiwo.com;
   	index index.php index.html;
    	ssl_certificate   /etc/nginx/cert/www.xiaolaiwo.com.pem;
    	ssl_certificate_key  /etc/nginx/cert/www.xiaolaiwo.com.key;


	if ($host != 'www.xiaolaiwo.com') {
        	rewrite ^/(.*)$ https://www.xiaolaiwo.com/$1 permanent;
        }
	
	gzip on;
	location ^~ /admin/ {
		root /var/www/xiaolaiwoBlog/apps/backoffice/public/;
	        try_files $uri $uri/ /admin/index.html;
    		#指定主页
    		index index.html;
    	}


        #后台接口系统
	location /api/ {
		alias  /var/www/xiaolaiwoBlog/apps/backoffice/public/;
		#try_files / /index.php$is_args$args;
		if (!-e $request_filename) {
			#return 200 $request_uri;
           		rewrite ^/api/(.*)$ /index.php/api/$1 last;
        	}
	}
     #图像过滤器,通过 PHP 对生成缩略图或压缩
	location ~ ^/media/cache/resolve/.+\.(png|jpeg|jpg|gif)$ {
                if (-f $request_filename) {
                        expires 365d;
                        access_log off;
		}
		add_header Cache-Control "public";
        	try_files $uri $uri/ /index.php$is_args$args;
        }
     #后台接口系统
	location ~ ^/index\.php/api/.*$  {
	        root /var/www/xiaolaiwoBlog/apps/backoffice/public/;
		fastcgi_pass unix:/var/run/php/php8.0-fpm.sock;
                fastcgi_split_path_info ^(.+\.php)(/.*)$;
                include fastcgi_params;
                fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
                fastcgi_param DOCUMENT_ROOT $realpath_root;
          # 隐藏index.php
                internal;
        }
     #内部重定向到index.php
        location / {
                root /var/www/xiaolaiwoBlog/apps/blog/public/;
                # try to serve file directly, fallback to index.php
                try_files $uri /index.php$is_args$args;
                gzip_static on;
        }
     # 调用PHP
    	location ~ ^/index\.php(/|$) {
		root /var/www/xiaolaiwoBlog/apps/blog/public/;
        	#fastcgi_pass php-upstream;
        	fastcgi_pass unix:/var/run/php/php8.0-fpm.sock;
        	fastcgi_split_path_info ^(.+\.php)(/.*)$;
        	include fastcgi_params;
        	fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        	fastcgi_param DOCUMENT_ROOT $realpath_root;
		# Prevents URIs that include the front controller. This will 404:
        	# http://domain.tld/app.php/some-path
        	# Remove the internal directive to allow URIs like this
        	internal;
    	}

	# 将静态文件缓存 30 天
        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|js|css)$ {
       		root /var/www/xiaolaiwoBlog/apps/blog/public/;
		if (-f $request_filename) {
		 	#root /var/www/xiaolaiwoBlog/apps/blog/public/;
			# Set expiry date to 1 year in the future.
            		expires 30d;

            		# Further optimize by not logging access to these files.
            		access_log off;
        	}
        }

	# return 404 for all other php files not matching the front controller
    	# this prevents access to other php files you don't want to be accessible.
    	location ~ \.php$ {
        	return 404;
    	}

    	error_log /var/log/nginx/xiaolaiwo_blog_error.log;
    	access_log /var/log/nginx/xiaolaiwo_blog_access.log;
}

server {
   # http 跳转到 https
    server_name xiaolaiwo.com www.xiaolaiwo.com;
    root /var/www/xiaolaiwoBlog/apps/blog/public;
    listen 80 default_server;

    return 301 https://$host$request_uri; # Redirect
}

重启Nginx,检查首页

https://www.xiaolaiwo.com/

发表评论