Use overlay functions (available in QGIS 3.16+): create a new attribute with Field calculator on the point layer, using the following expression. You get a field that contains for each point the value of the nearest polygon's fid
or an empty value if the current point is inside a polygon. Replace polygon
(lines 3 and 7) with the name of your polygon layer:
if (
overlay_disjoint(
'polygon'
),
array_first (
overlay_nearest (
'polygon',
$id
)
)
,''
)
Screenshot: labels show the value of the created field, corresponding to the id of the nearest polygon. Points inside a polygon don't show a label as the content is empty:
